使用while循环打印.txt文件

时间:2017-02-02 14:51:43

标签: java printing while-loop text-files

我试图打印整个.txt文件。我只能打印要打印的文件的第一行。我想我需要一个while循环来打印整个文件?从示例中我发现堆栈我只是感到困惑。这是我的代码。如何实现while循环打印" file1"?

由于

fileByteStream = new FileInputStream("Folder/mytext.txt");
inFS = new Scanner(fileByteStream);

file1 = inFS.next();

System.out.println( file1 );

1 个答案:

答案 0 :(得分:0)

 BufferedReader br = new BufferedReader(new FileReader("Folder\mytext.txt"));

 String line = null;

 while ((line = br.readLine()) != null) {
   System.out.println(line);
 }

重复https://stackoverflow.com/a/15696024/5224021