
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we create a string from the contents of a file in java?
We can use the method below to read the contents of a file in Java:
- Using the java.util.Scanner class.
- Using the java.io.BufferedReader class.
- Using the java.nio.file.Files class.
Using the Scanner class
In Java, you can read the contents of a file in several ways. One way is to read it into a string using the java.util.Scanner class, to do so, follow the steps below:
- Instantiate the Scanner class, with the path of the file to be read, as a parameter to its constructor.
- Create an empty String buffer.
- Start a while loop with a condition, if the Scanner has a next line. i.e. hasNextLine() at while.
- Within the loop, append each line of the file to the StringBuffer object using the append() method.
- Convert the contents of the buffer to a String using the toString() method.
Example
Create a file named sample.txt in the C directory in your system, and copy and paste the following content in it.
Tutorials Point originated from the idea that there exists a class of readers who respond better, online content and prefer, learn new skills at their own pace from the comforts of their drawing rooms.
Following is the Java program to read the contents of a file into a String using the Scanner class:
import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileToString { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(" "+input); } System.out.println("Contents of the file are: "+sb.toString()); } }
When the above code is executed, it produces the following output:
Contents of the file are: Tutorials Point originated from the idea that there exists a class of readers who respond better, online content and prefer, learn new skills at their own pace from the comforts of their drawing rooms.
Using the BufferedReader class
We can also read the contents of a file in Java using the BufferedReader class. The BufferedReader class is used to read the text from a character-based input stream.
The following are the steps to read the contents of a file using the BufferedReader class:
- Instantiate the BufferedReader class, with the path of the file to be read, as a parameter to its constructor.
- Create an empty String buffer.
- Start a while loop with a condition, if the BufferedReader has a next line. i.e. readLine() at while.
- Within the loop, append each line of the file to the StringBuffer object using the append() method.
- Convert the contents of the buffer to a String using the toString() method.
- Finally, close the BufferedReader object.
Example
Following is the Java program to read the contents of a file into a String using the BufferedReader class:
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileToString { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader(new File("E://test//sample.txt"))); String input; StringBuffer sb = new StringBuffer(); while ((input = br.readLine()) != null) { sb.append(" "+input); } System.out.println("Contents of the file are: "+sb.toString()); br.close(); } }
When the above code is executed, it produces the following output:
Contents of the file are: Tutorials Point originated from the idea that there exists a class of readers who respond better, on-line content and prefer, learn new skills at their own pace from the comforts of their drawing rooms.
Using the Files class
We can also read the contents of a file in Java using the java.nio.file.Files class. The Files class is used to read and write files in Java. The following are the steps to read the contents of a file using the Files class:
- Instantiate the Files class, with the path of the file to be read, as a parameter to its constructor.
- Create an empty String buffer.
- Use the readAllLines() method of the Files class to read all lines from the file.
- Within the loop, append each line of the file to the StringBuffer object using the append() method.
- Convert the contents of the buffer to a String using the toString() method.
Example
Following is the Java program to read the contents of a file into a String using the Files class:
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class FileToString { public static void main(String[] args) throws IOException { String input; StringBuffer sb = new StringBuffer(); List<String> lines = Files.readAllLines(Paths.get("E://test//sample.txt")); for (String line : lines) { sb.append(" "+line); } System.out.println("Contents of the file are: "+sb.toString()); } }
When the above code is executed, it produces the following output:
Contents of the file are: Tutorials Point originated from the idea that there exists a class of readers who respond better, on-line content and prefer, learn new skills at their own pace from the comforts of their drawing rooms.