All TalkersCode Topics

Follow TalkersCode On Social Media

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

Java Read File Into String

Last Updated : Mar 11, 2024

Java Read File Into String

In this article we will show you the solution of java read file into string, a text file can be written and read in many different ways.

This is necessary when working with numerous programmes. Java offers a number of methods for reading plain text files, including FileReader, BufferedReader, and Scanner.

The job is to read a file's contents from a local directory and save them in a string using a text file as a starting point.

Consider a file that is already on the system, let's suppose it is called "gfg.txt." Let the file's random content be as it is shown in the pretag block below.

We will now review several strategies for achieving the same goal. File "gfg.txt" contains the information depicted in the illustration block.

Files.lines()

Static methods for working with files and directories are available in the Files class.

Lines() is a helpful method that returns a stream of strings in the form of StreamString>. A file's lines can be obtained from this stream.

The procedure accepts a path to the desired file and an optional Charset.

To automate flushing and closing, we'll use the try-with-resources syntax

Files.readAllBytes()

The Files.readAllBytes() method, which returns a byte[, provides a more basic reading strategy.

The developer can use these bytes in any way they see fit, including converting them to a String or processing them directly.

Scanner

Scanner is a class that is especially helpful for reading data from streams.

It can be used to read strings because it operates with abstract streams.

Tokens from the input stream are successively collected by the scanner after it has broken the input into its component parts.

We want to employ methods that return strings because we are working with strings.

Next() and nextLine() in the scanner are specifically for that.

Both techniques return String-type objects. While the latter parses and returns full lines, the former is used to read any string.

The starting point and ending point of what we want to read are all parameters that the read() method accepts along with a sequence of characters (which we're using to store the read characters in).

We will read no more than 256 characters in total. Only 256 characters will be read from input.txt if there are more. The legible characters are returned if there are less.

How many characters were actually read by the procedure may be determined using the return value, which is kept inside the integer n.

The procedure returns -1 if the stream's end has been reached.

Step By Step Guide On Java Read File Into String :-

import java.io.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class GFG {
 public static void main(String[] args)
  throws IOException
 {
  Path fileName
   = Path.of("C:\\Users\\HP\\Desktop\\gfg.txt");
  String str = Files.readString(fileName);
                    System.out.println(str);
 }
}
  1. In the first step, we will import the pacakges, .IOexception, file.Files, file.Path.
  2. In the next step, we will create the Main class GFG.
  3. In the third step, we will create the Main Driver method.
  4. In the fourth step, we will choose a file from a local directory.
  5. In the next step we will call Files.readString() method to read the file.
  6. In the final step, printing the string using println(str).
  7. Run the code

Conclusion :-

So, in this section, we learned about reading files into strings in java.

In the previous section, we discussed reading files using Scanner, file reader, and buffer reader.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪