In this tutorial, you will learn how to Read in a File Line by Line in java. To understand it better, you may want to check the following first:
Java Program to Read in a File Line by Line
First, create a text file named javaCode.txt and write something on that text file. And then Save that text file within the same directory as the source code. For example, the following content is present in the javaCode.txt file.
javaCode.txt
Welcome to Simple2Code.
Let Start
first tutorial
second Program
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //read in a file line by line import java.io.*; public class FileRead { public static void main(String[] args) throws IOException { File f = new File("javaCode.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String s = null; while((s = br.readLine()) != null) { System.out.println(s); } fr.close(); } } |
Output:
Welcome to Simple2Code.
Let Start