This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The file pointer goes byte for byte. Lines and positions exist in the eye of the beholder: that is, it is due to the interpretation of the using programs. You can rewrite a portion of a file in the same length: if you want to insert more bytes, for example a longer line, then you have to rewrite the tail of the file.
Mukund Jahagirdar wrote:The above code is writing data at the end of file. How do I point the File pointer to a specific location say like line 2 char 20.
Personally, I wouldn't bother. I/O is extremely slow, and unless your file is absolutely gigantic (and I mean hundreds of megabytes), you can read the whole thing into memory and update it there. I'd also use a BufferedReader, since the class was designed specifically for reading text, and provides you with a readLine() method.
The only exception to this might be, if ALL the following conditions apply:
1. All updates involve a single line.
2. You can arrange them in line sequence.
3. You are outputting to a different file.
Then you could read in a line at a time, do your update, and write out the updated line. In such a case, you might want to look at a LineNumberReader instead.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joined: Feb 20, 2010
Actually i have a slight change in how do I want to update the file.
Consider the file which i have is something like this :
Now what i want to do is , i need to first look for the specific line say starting with MUK, once i get it , i need to update the Next 3 character say from 123 to 444 and then save the file with a diffferent file name.
I actually tried with String operation, where i am able to read the entire file and then find the String MUK, but i am not sure how do i move my cursor to the next line in the file and update it..
Below is my code:
Above code is actually printing out the entire file data and looking for the specified value "MUK" and then prints "java programming". Instead of printing i want to update the next 3 character in the file Can you provide some inputs ?
When you post code, please UseCodeTags (←click). And please read the page carefully, because it contains some other rules you should follow.
I've added them in for you this time, but you should correct your indentation (use the 'Edit' button).
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com