0

I have to display all of records in my blocks (text files), and do a split to "cover" the Fields Separators, but only the first record of my blocks are diplayed. What am I doing wrong?

enter code here 
public static void listAllStudents() throws IOException {

    File path = new File(Descriptor.getBlockPath());

    for (int i = 0; i < path.listFiles().length; i++) {

        try {
            FileInputStream file = new FileInputStream(Descriptor.getBlockPath() + "BLK" + i + ".txt");
            InputStreamReader entrada = new InputStreamReader(file);
            BufferedReader buf= new BufferedReader(entrada);
            String piece = " ";

            System.out.println("\nBLOCO " + i + " ------------------------------------------------------ +");

            do {


                if (buf.ready()) {
                    piece = buf.readLine();
                    System.out.println("\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨");

                    String string = " ", field[] = piece.split(Descriptor.getFieldSeparator());
                    string = " ";

                    System.out.println("CPF: " + field[0]);
                    System.out.println("Name: " + field[1]);
                    System.out.println("Course: " + field[2]);
                    System.out.println("Age: " + field[3]);
                    System.out.println("Phone: " + field[4]);
                    System.out.println("Active: " + field[5]);

                    string = " ";
                }

            } while (buf.ready());

            buf.close();
        } catch (IOException e) {
            System.out.println();
        }
    }

}
Mar Oli
  • 67
  • 8

1 Answers1

0

See the documentation for the BufferedReader.readLine() method:

or null if the end of the stream has been reached

Then change your code to read the file line by line:

while ((piece = buf.readLine()) != null) {
     String field[] = piece.split(Descriptor.getFieldSeparator());

     if (field.length >= 6) {
                System.out.println("CPF: " + field[0]);
                System.out.println("Name: " + field[1]);
                System.out.println("Course: " + field[2]);
                System.out.println("Age: " + field[3]);
                System.out.println("Phone: " + field[4]);
                System.out.println("Active: " + field[5]);
     }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Nope, have a read https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#ready() – Scary Wombat Oct 03 '16 at 01:15
  • Your link just tells that `#ready` depends on the reader implementation and OPs code falls back to `FileInputStream#available()` and this does tell if there is something left to read. – Tom Oct 03 '16 at 01:22
  • It says *Returns true if the next read() is guaranteed not to block for input,* - That's all it promises – Scary Wombat Oct 03 '16 at 01:26
  • Yes, you're right, let's limit our investigation to the JavaDoc, which obivously can't promise much, as this depends on the used `Reader` implementation. We shouldn't look into the reader (and the nested input stream) which OP actually uses. – Tom Oct 03 '16 at 01:38
  • @Tom 'Something available to be read without blocking' is not the same thing as 'something left to read'. Neither `ready()` nor `available()` is a valid test for end of stream, and the Javadoc specifically says so. – user207421 Oct 03 '16 at 03:27