只读取文件夹中的最后两个文本文件

时间:2013-10-13 16:30:02

标签: java file file-io decode java-io

在读取 decodeData(strLine)中的文本文件时,它只读取文件夹中的最后两个文本文件,并从第一个文本文件的某些部分开始(最后一个)。

private void readEachFile(String path) throws IOException {
    File[] dirs = new File(path).listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isFile() && file.canRead()) {
                System.out.println("File Name:" + file.getName());
                    try (BufferedReader br = new BufferedReader(new FileReader(file))) 
                    {
                    String strLine;
                    while ((strLine = br.readLine()) != null) {
                        System.out.println(strLine);
                        decodeData(strLine);//reading only last two files from the folder
                    }
                    return false;
                } catch (NullPointerException | FileNotFoundException fne) {
                    fne.printStackTrace();
                } catch (IOException | RuntimeException re) {
                    re.printStackTrace();
                } 
            }
            return file.isDirectory() && file.canRead();
        }
    });
    for (File dir : dirs) {
        readFile(dir.getCanonicalPath());
    }
}

因此,请提供阅读文件夹中所有文件的建议。

控制台看起来像这样:

AIS MessageType:1
Repeat Indicator:0
MMSI Number:4190000
AIS Version:1
IMO Number:134217819
Navigational status:Constrained by her draught
Rate Of Turn(ROT):128
Speed Over Ground(SOG):0
Position Accuracy(PA):Low or Default
Longitude:39.881207
Latitude:23.278917
Course Over Ground(COG):0
Heading(HDG):511
Time Stamp:The positioning system is inoperative

2013-01-04:18:30:09;!ABVDM,1,1,,A,14R3JV00235U=h:4?>fKpIT@0859,0*6A (Started reading from last but one text file at some line)
Raw Binary:000001000100100010000011011010100110000000000000000010000011000101100101001101110000001010000100001111001110101110011011111000011001100100010000000000001000000101001001

Decoded Data
Creation Time:2013-01-04:18:30:09
NMEA Message Type:!ABVDM
Fragments in the message:1
Fragment no:1
AIS MessageType:1
Repeat Indicator:0
MMSI Number:304143000
AIS Version:0
IMO Number:134233
Navigational status:Under way using engine
Rate Of Turn(ROT):0
Speed Over Ground(SOG):13
Position Accuracy(PA):Low or Default
Longitude:39.01739
Latitude:14.812778
Course Over Ground(COG):99
Heading(HDG):306
Time Stamp:The positioning system is inoperative

2013-01-04:18:30:09;!ABVDM,1,1,,A,16?a7t00005<Vcl<3?vDq5fB0<0l,0*55
Raw Binary:000001000110001111101001000111111100000000000000000000000000000101001100100110101011110100001100000011001111111110010100111001000101101110010010000000001100000000110100

Decoded Data
Creation Time:2013-01-04:18:30:09
NMEA Message Type:!ABVDM
Fragments in the message:1
Fragment no:1
AIS MessageType:1
Repeat Indicator:0
MMSI Number:419055600
AIS Version:0
IMO Number:83
Navigational status:Under way using engine
Rate Of Turn(ROT):0
Speed Over Ground(SOG):0
Position Accuracy(PA):Low or Default
Longitude:36.329273
Latitude:42.12051
Course Over Ground(COG):125
Heading(HDG):183
Time Stamp:The positioning system is inoperative
................................................
................................................

它从一些部分读取到文本文件的末尾(最后一个文本文件),并开始读取最后一个文本文件直到结束。

File Name: sample8.txt
2013-01-04:18:30:10;!ABVDM,1,1,,A,15RKV0002i4b=6@9eF15I4H@06Ap,0*4C
...................................................................

控制台上出现的输出已跳过这些字段而没有错误

strLine - Some line (started from some line from different text file)
Raw Binary:
Decoded Data
Creation Time:
NMEA Message Type:
Fragments in the message:
Fragment no:

在解码时从“AIS MessageType”字段开始

3 个答案:

答案 0 :(得分:2)

请注意,您在第一次while循环迭代期间关闭输入流,这就是您只读取您尝试读取的每个文件的第一行的原因。将br.close()语句移出循环,因为它关闭了输入流并释放了与该流关联的所有系统资源。

通常,首选方法是使用Java的7个try-with-resources语句,它将自动关闭资源并避免将来出现资源泄漏问题。

 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
        //do your logic here
 }

答案 1 :(得分:1)

如果你想要目录及其子目录中的所有文件,使用递归解决了这个问题:

private void fileScanner(String path) throws IOException {

    File[] dirs = new File(path).listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {

        if (file.isFile() && file.canRead()) {

            System.out.println(file.getName());
            BufferedReader br = new BufferedReader(new FileReader(file));
            String strLine;

            while ((strLine = br.readLine()) != null) {
                  System.out.println(strLine);
                  decodeData(strLine);
                  br.close();

                  return false; 
            }             
        }
        return file.isDirectory() && file.canRead();
        }
    });

    for (File dir : dirs) {

        fileScanner(dir.getCanonicalPath());
    }
}

答案 2 :(得分:1)

while ((strLine = br.readLine()) != null) {
    System.out.println(strLine);
    decodeData(strLine);//reading only first line of every text file
    br.close();
    return false;
}

close()调用和return语句必须在读取循环之外的}之后移动。

相关问题