Text file to String in one line

In Java there are lot of ways to read the files. You can use FileReader, BufferedReader etc. But sometimes you want to read text file and store it directly to the String. For example you have String with numbers which are there to provide some data to your program.

Now, reading each byte or every line and storing it to String is bit tedious when you want text content quickly from the file without writing much code.

In Java-7 with java.nio.file.Files you can have your whole text file in your String in just one line. For example,

String fileData = new String(Files.readAllBytes(new File("D:/abc.txt").toPath()));

Here you also need to handle the IOException or you can throw the IOException from your method. Now this will give you your file in String with just one line. You can also specify the encoding according to content of your text file as stated below.

String fileData = new String(Files.readAllBytes(new File("D:/abc.txt").toPath()), "UTF-8");

To make it more clear let’s divide it in different lines. Check following peace of code.

File file = new File("D:/abc.txt");
byte[] fileBytes = Files.readAllBytes(file.toPath());
String fileData = new String(fileBytes, "UTF-8");

Here first we have created on file and read all bytes with ‘nio’ Files and created String from that bytes.

I have used this trick in one point of programming where I had two files which have data in following format,

... and so on

I want to compare two files and find the common elements which are in both the files. Let me share my quick code only made to find the common values which can not be find by file comparator easily because values were not in any specific sequence.

try {
	String file1Str = new String(Files.readAllBytes(new File("D:/file1.txt").toPath()));
	String file2Str = new String(Files.readAllBytes(new File("D:/file2.txt").toPath()));
	List<String> file1List = Arrays.asList(file1Str.split(System.lineSeparator()));
	List<String> file2List = Arrays.asList(file2Str.split(System.lineSeparator()));
	Set<String> commonElements = new HashSet<>(file1List);
} catch (IOException e) {

It was quick enough that you can get difference between two files of thousands of line in few milliseconds. Note that this is just intended to solve the small problem quickly it might have taken me several minutes to manually find the duplicate elements. In real world programming it is not good idea to store whole file which can have lot of bytes in String and to be clear, that may consume your lot of heap space. But it is good to know about this one liner to have quick access to the file content.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s