I have the following problem. I have create a method that read a very big textual file starting by its path.
The file is read line by line and each line is appended into a StringBuffer.
This is my code:
public void run(Vector parametri) {
if (parametri != null && (parametri.isEmpty() == false)) {
gvParam = (Vector) parametri.clone();
} else {
TraceLog.scrivi("Test Esistenza Parametri", "Parametri mancanti", false, TraceLog.lowConsole + TraceLog.highTrace + TraceLog.highLog);
target.azione("Parametri mancanti !!");
return;
}
String fattureXml = gvParam.get(0).toString();
// READ THE FILE:
StringBuffer fileContent = new StringBuffer();
try {
// Creazione del reader per leggere il file:
BufferedReader br = new BufferedReader(new FileReader(fattureXml));
String line = null;
while ((line = br.readLine()) != null) {
//System.out.println(line);
fileContent.append(line);
}
System.out.println(fileContent.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The reading section begin after the comment // READ THE FILE:
The file read seems work fine because I used this line (actually commented)
System.out.println(line);
to check the readed line and it read it correctly.
As you can see now I am adding this line to a StringBuffer (to obtain a big String representing the content of the readed file), by:
fileContent.append(line);
The problem is that, after the loop, I am trying to print in the console the content of the fileContent StringBuffer, by:
System.out.println(fileContent.toString());
but nothing is printed.
Why? What am I missing? What exactly contains the fileContent StringBuffer? Is it something like a big String containing the lines of the readed file or what?
Here's my guess: Since you're discarding all new-line characters, you end up with a single enormously long line, which your console is having troubles handling. (I've experienced this myself in the Eclipse console.)
Try changing
to
As @assylias points out you also might want to drop
Vector
(and use for instance anArrayList
instead). Also, have a look at this question: Difference between StringBuilder and StringBuffer.For future reference, the "modern" way of doing this would be