Reading and Writing using a Large Random Access File
来源:互联网 发布:win7软件停止运行 编辑:程序博客网 时间:2024/04/28 01:55
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,orangeapple,redpeach,pink
and change it to this:
orange,orangegrapes,greenpeach,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.txtChar at 0=?Char at 2=?...Char at 28=?Char at 30=?Reached EOF!Line 1 File Position=0Done
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.
- Reading and Writing using a Large Random Access File
- PHP Reading A File |and| Writing To A File
- Writing and Reading and Deleting a Cookie on the Browser
- Reading and Writing Logs
- Reading and Writing Logs
- Robust Reading and Writing
- Reading and Writing Files
- Improving performance of reading text from a large text file
- 【DataStructure】A useful util class for reading and writing files
- Creates a pipe for reading and writing的例子程序
- 关于Thinking,Reading and Writing
- Reading and writing text files
- 1.7writing and reading files
- Reading and writing text files
- Reading and writing binary files
- Reading and writing Serializable objects
- Reading and writing text files
- reading and writing xml files
- ffmpeg 使用滤镜
- 在 XML 中通过数据绑定用一行代码定制字体
- 初识移动端跨平台(JavaScript篇)
- Dubbo架构设计详解
- 教主Baby大婚能收多少份子钱?
- Reading and Writing using a Large Random Access File
- 高度平衡
- 成为核心程序员的一些建议
- 如何在lua中打印一个数组(table)
- PHP CLI 模式详解
- nodeJs mongodb凭据验证
- LintCode: 最大子数组
- 根据两点经纬坐标计算两点间的距离[JAVA]
- JAVA模拟微信消息发送请求