• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BufferedWriter Problem

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to copy a file into a different file after cutting off the last few lines based on a check.
The problem I am encountering is that the final file is shorter than the original and cutting off portions that should be there.
The original file is of length 779KB and the new file is only 768KB.
Here is the code.
any help will be appreciated.

BufferedReader bufIn = new BufferedReader(new FileReader(currFile)) ;
File outFile = new File("out.txt");
if(!outFile.exists()) {
outFile.createNewFile();
}
BufferedWriter bufOut = new BufferedWriter(new FileWriter(outFile)) ;

String lineByLine = bufIn.readLine();
while (lineByLine!=null){

lineByLine = bufIn.readLine();
bufOut.write(lineByLine+"\n");
if (lineByLine.startsWith("T837")){
break;
}//end if
}//end while
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, two things. First of all, using "write" to put a newline into a file will put only one newline (ASCII 10) character into the file; it won't put the platform-appropriate line-ending characters into the file. If you're on UNIX, there's no problem. If it's a Windows file you're reading, then you'll lose one character for each line, because Windows text files use two characters (0x0a, 0x0d) at the end of each line. Either use the line.separator system property, or use a PrintWriter wrapped around the BufferedWriter (or instead of it) and use "println()" which will do the right thing automatically.

The second issue is that you don't close the file in the code here; until you call close() on the BufferedWriter, the file may not contain everything you've written to it. The balance of the text is still in the BW's buffer.

Finally, this isn't advanced Java. I'm going to move this thread to our I/O forum.
 
Karthik Krishnamurthy
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
That worked.
Sorry for the cross posting.

Karthik
 
reply
    Bookmark Topic Watch Topic
  • New Topic