Odd Java Problem -File I/O

Soldato
Joined
22 Oct 2005
Posts
2,883
Location
Moving...
I'm trying to wrte some data to a file using the following code:

Code:
public static void buildFile(int lat, int lon) throws IOException{
        
        String tempLat;
        String tempLon;
        String line;
        
        if (lat <10)
            temp = "G:\\South America 300(1)\\N0" + lat + "W0" + lon +".txt";
        else
            temp = "G:\\South America 300(1)\\N" + lat + "W0" + lon +".txt";
        
        
        File inFile = new File("North.txt");
        FileReader input = new FileReader(inFile);
        BufferedReader bufRead = new BufferedReader(input);
         
        File outFile = new File(temp);
        PrintWriter out = new PrintWriter(new FileWriter(outFile));
        
        line = bufRead.readLine(); // Read first line
        
        while (line != null){
            
            tempLon = line.substring(3,10);
            tempLat = line.substring(11,18);
            out.println("-" + lon + tempLon + lat + tempLat + "0");
            
            line = bufRead.readLine();
        }     
    }

There are 90,000 lines in the input file so there should be 90,000 in the output, but there isn't, there's only 89,772. I have absolutely no idea why its missing the end off.

I've got several other programs which use very similar code working with file sizes from 1MB to 500MB, so file size isn't the problem. I've also tried writing a .dat instead of a .txt but same result. I've also tested it with multiple input files but it gives the same result. I've also checked there isn't anything funny going on with line 89,772 in the input file (missing line, extra characters etc) but it all looks fine. I've also debugged the code and it's reading all the way up to 90,000 lines and its even executing the "out.println("-" + lon + tempLon + lat + tempLat + "0");" line with the very last line in the input file so its executing the code correctly by the looks of it, it just doesn't want to output everything to the text file.

Any ideas whats going wrong?
Thanks.
 
Make sure your closing the streams after you have done (this should flush the buffers).

out.close(); etc..
 
That was it Una, thank you very much, I was closing it but it was in the wrong place. I hate it how I always miss the silly little things!

Thanks again :)
 
That was it Una, thank you very much, I was closing it but it was in the wrong place. I hate it how I always miss the silly little things!

Thanks again :)

Aye because its buffered i/o it depends on the size of writes to the buffers. So if you still have data in the buffer which has not been written to the file you need to flush it first. close() automatically does that for you.
 
This is more personal preference over anything, but I prefer to do the following:
Code:
/*
 * just looks more tidy to me if there is only one bufRead.readLine()
 */
while ((line = bufRead.readLine()) != null){        
  tempLon = line.substring(3,10);
  tempLat = line.substring(11,18);
  out.println("-" + lon + tempLon + lat + tempLat + "0");
}
bufRead.close();
out.close();
 
Back
Top Bottom