Showing posts with label BufferedReader. Show all posts
Showing posts with label BufferedReader. Show all posts

Sunday, 27 November 2022

Read file content using BufferedReader in Java

Step 1: Get the instance of FileReader.

final FileReader fileReader = new FileReader(filePath);

 

Step 2: Get the instance of BufferedReader from FileReader.

final BufferedReader bufferedReader = new BufferedReader(fileReader);

 

Step 3: Read the file content using read method of BufferedReader.

final StringBuilder stringBuilder = new StringBuilder();
final char[] charBuffer = new char[65535];
int noOfCharsRead;

while ((noOfCharsRead = bufferedReader.read(charBuffer)) != -1) {
	stringBuilder.append(charBuffer, 0, noOfCharsRead);
}

 

Find the below working application.


FileUtil.java

package com.sample.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileUtil {

	public static String readText(final String filePath) throws FileNotFoundException, IOException {
		if (filePath == null || filePath.isEmpty()) {
			return "";
		}

		try (final FileReader fileReader = new FileReader(filePath);
				final BufferedReader bufferedReader = new BufferedReader(fileReader)) {

			final StringBuilder stringBuilder = new StringBuilder();

			final char[] charBuffer = new char[65535];

			int noOfCharsRead;
			while ((noOfCharsRead = bufferedReader.read(charBuffer)) != -1) {
				stringBuilder.append(charBuffer, 0, noOfCharsRead);
			}
			return stringBuilder.toString();
		}
	}

}

FileReadDemo.java

package com.sample.app;

import java.io.IOException;

import com.sample.util.FileUtil;

public class FileReadDemo {

	public static void main(final String[] args) throws IOException {
		final String filePath = "/Users/Shared/a.txt";

		System.out.println(FileUtil.readText(filePath));
	}

}

 

You may like

file and stream programs in Java

How to download a binary file in Java?

How to process a huge file line by line in Java?

How to get the directory size in Java?

Convert string to InputStream in Java

Read the data from BufferedReader in Java

Read the data from BufferedReader in Java

BufferedReader class read the text from a character-input stream. As it read the characters into a buffer, it is very efficient while reading the file data.

 

Find the below working application.


 

IOUtil.java

package com.sample.util;

import java.io.BufferedReader;
import java.io.IOException;

public class IOUtil {

	public static String readText(final BufferedReader bufferedReader) throws IOException {

		if (bufferedReader == null) {
			return "";
		}

		final StringBuilder stringBuilder = new StringBuilder();

		final char[] charBuffer = new char[65535];

		try {
			int noOfCharsRead;
			while ((noOfCharsRead = bufferedReader.read(charBuffer)) != -1) {
				stringBuilder.append(charBuffer, 0, noOfCharsRead);
			}
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e) {
				System.out.println("Failed to close the reader : " + e.getMessage());
			}
		}
		return stringBuilder.toString();
	}

}

FileReadDemo.java

package com.sample.app;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.sample.util.IOUtil;

public class FileReadDemo {

	public static void main(final String[] args) throws IOException {
		final BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/Shared/a.txt"));

		System.out.println(IOUtil.readText(bufferedReader));
	}

}



You may like

file and stream programs in Java

Write byte array to a file

How to download a binary file in Java?

How to process a huge file line by line in Java?

How to get the directory size in Java?

Convert string to InputStream in Java

Tuesday, 12 July 2022

How to process a huge file in Java?

Problem statement

Assume you have given a file of huge size and process the file line by line and write the data to some destination.

 


Point to consider from problem statement

As per the problem statement, we need to process the file line by line. By considering this, we no need to load all the file content to in-memory. We can read the file line by line and process the line, write the processed result to an external destination.

public static void processFile(String inputFilePath, String outputFilePath,
		Function<String, String> lineTransformation)
		throws FileNotFoundException, IOException {
	try (BufferedReader reader = new BufferedReader(new FileReader(new File(inputFilePath)));
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputFilePath)))) {
		String line;

		while ((line = reader.readLine()) != null) {
			writer.write(lineTransformation.apply(line));
			writer.write("\n");
		}
	}
}

 

Above snippet read the file line by line, apply the lineTransformation on each line and write the result to destination.

 

Find the below working application.

 

FileUtil.java
package com.sample.app.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.function.Function;

public class FileUtil {

	public static void processFile(String inputFilePath, String outputFilePath,
			Function<String, String> lineTransformation)
			throws FileNotFoundException, IOException {
		try (BufferedReader reader = new BufferedReader(new FileReader(new File(inputFilePath)));
				BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputFilePath)))) {
			String line;

			while ((line = reader.readLine()) != null) {
				writer.write(lineTransformation.apply(line));
				writer.write("\n");
			}
		}
	}

}

 

App.java

package com.sample.app;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.function.Function;

import com.sample.app.util.FileUtil;

public class App {
	public static void main(String[] args) throws FileNotFoundException, IOException {

		Function<String, String> toUpper = new Function<String, String>() {

			@Override
			public String apply(String t) {
				return t.toUpperCase();
			}

		};

		FileUtil.processFile("/Users/Shared/a.txt", "/Users/Shared/b.txt", toUpper);
	}
}

 

 

You may like

File programs in Java

Download file from Internet using Java

Get the content of resource file as string

Copy the content of file to other location

Write byte array to a file

How to download a binary file in Java?