• Post Reply Bookmark Topic Watch Topic
  • New Topic

Update the character on file writing.  RSS feed

 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have requirement to copy content from one file to another. So is my below code. But the additional requirement is that the second character in every line should be checked and changed to character 'X'. Please let me know how can this be checked.


 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshwar Prasad wrote:

That's going to give you problems. read moves the file's internal pointer forward, so some data will be read but never used.

You mention lines. Then you shouldn't read byte[] per byte[]. Instead, switch to something that knows how to read lines; a BufferedReader around a FileReader perhaps. (Don't forget to switch from FileOutputStream to FileWriter as well.)

Then read the file line by line, modify the line (warning: what if the line has 0 or 1 characters?) and write it to the file (using FileWriter). Don't forget to add new line breaks yourself, because most (all?) line reading classes strip away the line breaks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!