Java – how to read BufferedReader two or more times?
•
Java
I have a text file, one integer per line –
10 20 50
I want to read and print these numbers twice or more I tried some code and failed How do I change the code to print the list twice?
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; public class DoubleBuffer { public static void main(String[] args) { try { FileInputStream fstream = new FileInputStream("c:/files/numbers.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine; // Read rows while ((strLine = br.readLine()) != null) { System.out.println(strLine); } // Read rows again while ((strLine = br.readLine()) != null) { System.out.println(strLine); } in.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); }//try-catch }// main }// class
Solution
You can now print multiple times
BufferedReader br = new BufferedReader(new FileReader( "D:/log_2071-04-31.txt" )); String strLine; ArrayList<String> ans= new ArrayList<String>(); // Read rows while ((strLine = br.readLine()) != null) { System.out.println(strLine); ans.add(strLine); } // Read again for (String result: ans) { System.out.println(result); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码