Java Program to Create String from Contents of a File
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 :
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- 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.
- 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.
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.
- 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.
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.
- 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.
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.
- 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.