Read a file line by line in Java

Method 1:

private static void readFile1(File fin) throws IOException { FileInputStream fis = new FileInputStream(fin); //Construct BufferedReader from InputStreamReader BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); }

Method 2:

private static void readFile2(File fin) throws IOException { // Construct BufferedReader from FileReader BufferedReader br = new BufferedReader(new FileReader(fin)); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); }

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s