aspose file tools*
The moose likes Java in General and the fly likes Java File Operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java File Operation" Watch "Java File Operation" New topic
Author

Java File Operation

Mukund Jahagirdar
Greenhorn

Joined: Feb 20, 2010
Posts: 6
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

Joined: Sep 20, 2010
Posts: 3394
    
    9
What have you tried?
Mukund Jahagirdar
Greenhorn

Joined: Feb 20, 2010
Posts: 6
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

Joined: Sep 20, 2010
Posts: 3394
    
    9
If you show us your code, we'll be able to help you a bit better.
Mukund Jahagirdar
Greenhorn

Joined: Feb 20, 2010
Posts: 6
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

Joined: May 22, 2012
Posts: 865
    
    5
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

Joined: Mar 17, 2011
Posts: 7081
    
  16

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

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Mukund Jahagirdar
Greenhorn

Joined: Feb 20, 2010
Posts: 6
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

Joined: Mar 17, 2011
Posts: 7081
    
  16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java File Operation
 
Similar Threads
Reading through Files in a Directory
Flagging duplicate lines when comparing the input from two seperate text files
I/O
Please help
Overwriting Objects in ArrayList