• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java File Operation

 
Mukund Jahagirdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Below is what i am looking at :

I want to read a specific text file which has multiple Lines in it.

Now read a particular character in the file, say from line 2 and update the position(10-20 on line 2 ) with some other value and save the file to a new different file.


Can you please help me on this ?
 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
 
Mukund Jahagirdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have tried using randomfileaccess method. using this i am only able to view get the first line data , I am not able to move to next line in the file..
 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you show us your code, we'll be able to help you a bit better.
 
Mukund Jahagirdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;

public class randomAccess {

private static void doAccess() {

try {

File file = new File("testfile.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");

// Read a character
byte ch = raf.readByte();
System.out.println("Read first character of file: " + (char)ch);

System.out.println("Read full line: " + raf.readLine());


raf.writeBytes("Writing here ");



raf.close();

} catch (IOException e) {
System.out.println("IOException:");
e.printStackTrace();
}
}

public static void main(String[] args) {
doAccess();
}

}


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.
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Mukund Jahagirdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

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 :

MUK123
MJM456
MOM784

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 ?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukund Jahagirdar wrote:Below is my code:

Mukund,

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

Thanks

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic