Read a File in Java Example using BufferReader

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

public class ReadMyFileExample {

	public static void main(String[] args) {

		// Use StringBuffer to append the file content
		StringBuffer myFileContents = new StringBuffer();

		BufferedReader br = null;
		String line = null;

		try {
			File file = new File("C:\\readMe.txt");

			br = new BufferedReader(new FileReader(file));

			while ((line = br.readLine()) != null) {
				myFileContents.append(line + "\n");
			}

			// print the file content
			System.out.println(myFileContents.toString());

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

	}
}

						
Output
Hello, you are reading file content.


















Receive Latest Java Examples in Your Email: