简体   繁体   English

为什么我的方法不能读取我的所有文件? …Java?

[英]Why my method don't read all of my File? … Java?

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();
        }
    }

}

See the documentation for the BufferedReader.readLine() method : 请参阅BufferedReader.readLine()方法的文档:

or null if the end of the stream has been reached 如果已到达流的末尾,则返回null

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]);
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM