Reading and Writing using a Large Random Access File

I have a delimited file where each line is of a varying length and can have a varying amount of delimiters. I need to access a random line and replace it with a new line that may be shorter or longer in length. Because the file is large (20MB+), I need to do this as efficiently as possible.

I started with trying to write a simple program that would take a line number, a string, and a file, move to the designated line number, and replace the existing line with the new string. I wanted to scan through the file and after n number of end line characters were reached, write the string after that position.

For example, the program below is supposed to take a file like this:

orange,orange
apple,red
peach,pink

and change it to this:

orange,orange
grapes,green
peach,pink

The output of my program is below. As you can see, I can’t even seem to get a char to read properly. What am I doing wrong?

Path=F:\Eclipse\Workspace\FlashCards\test.txt
Char at 0=?
Char at 2=?
.
.
.
Char at 28=?
Char at 30=?
Reached EOF!
Line 1 File Position=0
Done
import java.io.File;
import java.io.RandomAccessFile;

public class FileIOTest {

    public static void main(String[] args) throws Exception{
        String entry = "grapes,green";
        int line = 1;
        File file = new File("test.txt");
        RandomAccessFile f = new RandomAccessFile(file, "rw");

        long filePointer=0;

        System.out.println("Path="+file.getCanonicalPath());
        try{
            int n=0;
            while(n<line){
                long p = f.getFilePointer();
                char c = f.readChar();
                System.out.println("Char at "+p+"="+c);
                if(c=='\n'){
                    n++;        
                }

            }
        }catch(Exception e){
            System.out.println("Reached EOF!");
        }
        System.out.println("Line 1 File Position="+filePointer);
        f.seek(filePointer);
        f.writeBytes(entry);
        f.seek(0);
        f.close();
        System.out.println("Done");
    }
}

When you call readChar(), your program is actually reading two bytes - since a char is two bytes in size. readChar() should only be used when the value has been written using writeChar().

RandomAccessFile is not a good class for this problem. It cannot add to a file or remove from a file - it can only replace bytes with an equal amount of bytes. That’s not what you specified. Check out FileReader and FileWriter instead. You can’t read from and write to the same file at the same time though, so you have to either a) read everything first, or b) write to a new file first, then delete the original and rename the new file.

After doing some more research on Random Access Files I just scrapped that idea. I ended up doing what you suggested instead and just reading the whole file in, working with it in memory, and saving it at the end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值