0

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?

4
  • Are you sure no exception is thrown? Jun 9, 2015 at 15:02
  • 1
    (1) You could use a debugger and inspect the variables in the loop to see what's going on. (2) Vector and StringBuffer are osbolete (3) You should use generic instead of raw types (Vector<SomeType> instead of just Vector) (4) it may be a problem with your console not being able to print such a large string at once....
    – assylias
    Jun 9, 2015 at 15:04
  • @assylias I am working on a very very old legacy application (welcome back in the 90s...) and I can't change it :-( Jun 9, 2015 at 15:06
  • @AndreaNobili Ooops - you have all my sympathy :-)
    – assylias
    Jun 9, 2015 at 15:07

1 Answer 1

4

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

fileContent.append(line);

to

fileContent.append(line).append('\n');

As @assylias points out you also might want to drop Vector (and use for instance an ArrayList instead). Also, have a look at this question: Difference between StringBuilder and StringBuffer.

For future reference, the "modern" way of doing this would be

String content = new String(Files.readAllBytes(Paths.get(fileName)));
1
  • Wild but reasonable guess :-)
    – assylias
    Jun 9, 2015 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.