0

While reading the text files in decodeData(strLine),it is reading only last two text files in the folder and started from some part of first text file(last but one).

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

So please give suggestions for the reading all the files from the folder.

Console is looking like this:

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
................................................
................................................

It read from some part to end of text file(last but one text file), and started reading the last text file till end.

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

And the output which is appeared on the console which has skipped these fields with no error

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:

And started from "AIS MessageType" field while decoding

spt
  • 421
  • 4
  • 12
  • 27

3 Answers3

2

Note that you are closing your input stream during the first while loop iteration, this is the reason that you read only the first line of each file that you are trying to read. Move the br.close() statement outside the loop, since it's closes the input stream and releases any system resources associated with the stream.

Generally, the preferred way is to use Java's 7 try-with-resources statement, that will close your resource automatically and avoid you from resource leaking problems in the future.

 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
        //do your logic here
 }
Maxim Kirilov
  • 2,639
  • 24
  • 49
1

If you want all the files from the directory and its sub directories, this solved using recursion:

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());
    }
}
Anton Dozortsev
  • 4,782
  • 5
  • 34
  • 69
  • It is reading only the first line of every file in the decodeData(strLine) method. – spt Oct 14 '13 at 04:37
  • Make up your mind. It is only reading some files? or it is only reading one line of all files? or is it both? – user207421 Oct 14 '13 at 04:58
  • @EJP While working it recursively. It is reading only the first line of every file. – spt Oct 14 '13 at 05:09
1
while ((strLine = br.readLine()) != null) {
    System.out.println(strLine);
    decodeData(strLine);//reading only first line of every text file
    br.close();
    return false;
}

The close() call and the return statement must be moved after the }, outside the read loop.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I tried it, but it reading only the last two files. And starting from one line from some text file. – spt Oct 14 '13 at 06:41
  • I changed, but it is reading the last two text files.The console which started at some line from some text. In the first line it skipped some fields and reading it.The second strLine which is read from last but one text file from some line. – spt Oct 14 '13 at 06:44
  • I can't make head or tail of that, but it will only process files for which your accept() method returns true. Have you considered tracing that? – user207421 Oct 14 '13 at 09:14
  • @EJB How to trace it. It is also reading the strLine many times. – spt Oct 14 '13 at 09:26
  • That code once corrected as per my answer will read the entire file. If it doesn't, you haven't fixed it properly. 'Trace' means adding some debugging println() calls, in this case inside the accept() method. I shouldn't have to explain basic things like that. – user207421 Oct 20 '13 at 02:01