All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Write Java Program To Read And Write String Using File

Last Updated : Mar 11, 2024

Write Java Program To Read And Write String Using File

In this article we will show you the solution of write java program to read and write string using file, a text file can be written and read in a variety of ways. This is necessary when dealing with a large number of applications.

In Java, there are various methods for reading a plain text file, such as FileReader, BufferedReader, or Scanner.

The task, given a text file, is to learn the contents of a file in a local directory as well as store it in a string.

Consider the file 'gfg.txt', which is present on the system.

Let the random information in the file be as shown in the pretag block below.

Now we'll talk about different ways to accomplish the same thing. The contents of the file 'demo.txt' are depicted inside the illustration block.

  • Using the method File.readString()
  • Using the BufferReader class's readLine() method
  • Using the File.readAllBytes() function
  • Using the File.lines() function
  • Making use of the Scanner class

To understand them, let us communicate each of them while implementing clean java programmes.

Utilizing the File is Method 1. function readString()

For read the content of the a given file in Java, utilise the readString() method of the File Class.

Syntax:

Parameters: Files.readString(filePath); Path to a file with the data type Path

Return Value: The material of the folder in String format is returned by this method.

Method 2: Using the BufferReader class's readLine() method

BufferedReader is a text-reading object that reads text from such a character-input stream.

The BufferReader method's readLine() method is utilized to read this same file one line at the time and revert back the content.

Syntax:

public IOException is thrown by string readLine().

Parameters: No parameters are accepted by this method.

Value returned: This function compares the string read by this method, excluding any termination symbols. The above method returns NULL if the buffered stream must have ended and there are no lines to read.

BufferedReader is a text-reading object that reads text from the a character-input stream.

The BufferReader method's readLine() method is employed to read this same file one line at the time and revert back the content.File.

All bytes from a directory are read using the readAllBytes() function.

The method makes sure that the file is now closed when all bytes have actually been read, an I/O error, or another runtime exception is raised.

Following the reading of all bytes, we pass those bytes towards the string class function Object() { [native code] } in order to create a string.

exceptions

ArgumentException: The path is either a zero-length string or contains one or maybe more incorrect characters as described by

InvalidPathChars.

The path is null, resulting in an ArgumentNullException.

PathTooLongException: The defined path, file name, or even both exceed the maximum length set by the system.

The specified path is invalid, resulting in a DirectoryNotFoundException.

An IOException has been raised because an I/O error happened when attempting to open the file.

UnauthorizedAccessException: The current platform does not support this operation. OR the path indicated a directory. OR the caller lacks the necessary authorization.

The file indicated in the path wasn't really found, resulting in a FileNotFoundException.

The path has an invalid format, resulting in a NotSupportedException.

SecurityException: The caller lacks the necessary permissions.

Step By Step Guide On Write Java Program To Read And Write String Using File :-

import java.io.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class TalkersCode{
    public static void main(String[] args)
        throws IOException
    {
        Path fileName
            = Path.of("C:\\Users\\HP\\Desktop\\demo.txt");
        String str = Files.readString(fileName);
        System.out.println(str);
    }
}
  1. First, we start creating an import function called java.io.*
  2. The following step involves creating a class called TalkersCode.
  3. After which we throw an exception during the program's creation.
  4. Then we assign the path to the file's content.
  5. After that, we define the file's name.
  6. We then use files.readstring to create a string.
  7. Finally, we use system.out.println to exit the program.

Conclusion :-

I hope this article on write java program to read and write string using file helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪