I have a text file with one integer per row -
10
20
50
I want to read and print these numbers twice or maybe even multiple times. I tried some code and it failed. How do I change my 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
Now you can 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);
}
You can use mark()
and reset()
or just open the file again. Depending on your use, perhaps you want to store the data in memory?
Reference - http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#reset%28%29
There is another possibility: Attach listeners to a MultiReader
.
interface Listener {
void read(String line);
}
class MultiReader {
final List<Listener> listeners = new ArrayList<Listener>();
public void addListener(Listener l) {
this.listeners.add(l);
}
public void read(File file) throws IOException {
final BufferedReader br = new BufferedReader(new FileReader(file));
try {
String line;
while ((line = br.readLine()) != null) {
for (Listener l : this.listeners) {
l.read(line);
}
}
} finally {
br.close();
}
}
}
to be used like this:
public class MultiBufferedReader {
public static void main(String[] args) throws IOException {
MultiReader mr = new MultiReader();
mr.addListener(new Listener() {
@Override
public void read(String line) {
System.out.println("1: Got " + line);
}
});
mr.addListener(new Listener() {
@Override
public void read(String line) {
System.out.println("2: Got " + line);
}
});
mr.read(new File("/etc/hosts"));
}
So the file is read once, and it can handle large files, because the file's content is not held in memory.
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);
}
// return stream to top of file
fstream.getChannel().position(0);
br=new BufferedReader(new InputStreamReader(fstream));
// 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
Reading same stuff multiple times will lead to poor performance...
Unless you have a very strong reason to make multiple reads, read at the beginning of main method and store the data in a suitable data structure for your requirement.