Java Program to Create String from Contents of a File
Last Updated :
17 Nov, 2020
A File is a computer resource which is deployed to store different types of data such as text, image, video, to name a few. It is basically a collection of data bound to a single entity. While using your computer, it becomes essential to be able to deal with files and in this article we will be learning about various ways to read data from files. We will be using the File class extensively for the same. After reading the contents of the file, we will be storing them in a string which is typically an array of characters. For more information on String class, click here.
There are 4 methods by which we can read the contents of a file and convert them into a string in Java. The 4 approaches are mentioned below :
- Using readString() method of the Files class
- Reading the contents of the file in the form of a bytes array and then converting it into a string
- Reading the file line by line using the BufferedReader class
- Storing the contents of the file in the form of a stream and then making a string from it
Using this approach, we deploy the readString() function.
Algorithm :
- Read the path of the file as a string.
- Convert the string into a Path variable.
- Give this Path variable as a parameter to readString() function.
- Return the string to the main function.
Implementation :
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class GFG {
public static String fileToString(String p) throws IOException
{
Path path = Paths.get(p);
String contents = Files.readString(path);
return contents;
}
public static void main(String[] args)
throws IOException
{
System.out.print(fileToString( "/home/mayur/GFG.java" ));
}
}
|
Approach 2 :
Using this approach, we first read the contents of the file and store them as a byte array. Finally, we convert it into a String.
Algorithm :
- First, pass the path as a String variable to the function.
- Next, convert it into a Path Variable.
- Pass this variable as a parameter to readAllBytes() function of the Files class.
- Next, convert this array to a String.
Implementation :
Java
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
class GFG {
public static String fileToString(String p)
{
Path path = Paths.get(p);
byte [] b = {};
try {
b = Files.readAllBytes(path);
}
catch (IOException e) {
e.printStackTrace();
}
String contents = new String(b);
return contents;
}
public static void main(String[] args)
{
System.out.print(fileToString( "/home/mayur/GFG.java" ));
}
}
|
Approach 3 :
Using this approach, we read the contents of the file one line at a time till we find a null character using readLine() function of the BufferedReader class.
Algorithm :
- Pass the String variable containing the path to the file while calling the fileToString() method
- Initialize a FileReader object with the path variable as the parameter.
- Read the contents of the file using BufferedReader class.
- Stop when you see a null character.
- Store the contents in a String while appending the new line character after every line accepted.
Implementation :
Java
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
class GFG {
public static String fileToString(String p)
{
String contents = "" ;
try (FileReader f = new FileReader(p)) {
BufferedReader br = new BufferedReader(f);
String current = "" ;
while ((current = br.readLine()) != null )
contents += current + "\n" ;
}
catch (IOException e) {
e.printStackTrace();
}
return contents;
}
public static void main(String[] args)
{
System.out.print(
fileToString( "/home/mayur/GFG.java" ));
}
}
|
Approach 4 :
Using this approach, we read the contents of a file and store them in Stream of String. Next, we convert this Stream into a String array and iterate through it. We store all the strings in a single string created by a StringBuffer class.
Algorithm :
- Passing the string path to the function fileToString().
- Convert this string to a Path variable.
- Initialize a StringBuffer object to store the final string.
- Read the contents and store them in a Stream of String.
- Convert this String Stream into a String array.
- Iterate through this array and store all the strings in the StringBuffer object.
- Return the final string.
Implementation :
Java
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.charset.*;
import java.util.stream.*;
class GFG {
public static String fileToString(String p)
{
Path path = Paths.get(p);
StringBuffer s = new StringBuffer();
String contents = "" ;
try (Stream<String> str
= Files.lines(path, StandardCharsets.UTF_8)) {
String[] arr
= str.toArray(size -> new String[size]);
for (String string : arr) {
contents += string + "\n" ;
}
}
catch (IOException e) {
e.printStackTrace();
}
return contents;
}
public static void main(String[] args)
{
System.out.print(
fileToString( "/home/mayur/GFG.java" ));
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...