I have to use the file as the database , But I am confused that how to insert the data into file . It is very stupid that copy the file and add the new data then rewrite a new file. And I notice that many database have store the data into the file and they both write in C/C++ . And I wonder how to achieve same function in Java. But I had try many times , use RandomAccessFile and FileChannel to insert the data . But it just overwrite the data at the position i want to insert .Some inspire idea will be help! Thanks :)!
我必须使用该文件作为数据库,但我很困惑,如何将数据插入到文件中。复制文件并添加新数据然后重写新文件是非常愚蠢的。我注意到许多数据库已将数据存储到文件中,并且它们都是用C / C ++编写的。我想知道如何在Java中实现相同的功能。但我尝试了很多次,使用RandomAccessFile和FileChannel来插入数据。但它只是覆盖我要插入的位置的数据。有些启发想法会有所帮助!谢谢 :)!
Here is code I had ever write.But it overwrite ! overwrite!
这是我写过的代码。但它覆盖了!覆盖!
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Reader {
public static void main(String[] args) throws IOException {
new File("a.txt").createNewFile();
FileChannel fc = FileChannel.open(Paths.get("a.txt"),StandardOpenOption.WRITE);
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put("one\ntwo\nthree\nfour\n".getBytes());
buf.flip();
fc.write(buf);
//set the pos to insert the data
//I want to insert the data after three the pos is 14
fc.position(14);
//clear the buf and add the new data
buf.clear();
buf.put("five\n".getBytes());
buf.flip();
fc.write(buf);
fc.close();
}
}
2 个解决方案
#1
2
There is no simple way to "insert" a line in the middle of a file. Filesystems and I/O subsystems don't work that way. To truly insert a line you have to copy the file and add the line in the right place as you are copying.
没有简单的方法可以在文件中间“插入”一行。文件系统和I / O子系统不能以这种方式工作。要真正插入一行,您必须复制文件,并在复制时将行添加到正确的位置。
You say "...many database have store the data into the file ..." -- this is true, but they do it with sophisticated block-level techniques where they maintain chains of blocks on disk and update pointers to make it look like the rows are inserted. A lot of work goes into making all this transparent to the database user.
你说“......许多数据库都将数据存储到文件中......” - 这是事实,但是他们使用复杂的块级技术来实现这一点,它们在磁盘上维护块链并更新指针以使其看起来像就像插入行一样。许多工作都是为了让所有这些对数据库用户透明。
Writing even a simple "database" that can insert data in the middle of a file is a significant amount of work.
甚至编写一个可以在文件中间插入数据的简单“数据库”也是一项重要的工作。
#2
1
You cannot insert in the middle of a file. C/C++ cannot do that either.
您无法在文件中间插入。 C / C ++也不能这样做。
To insert in the middle of a file, the rest of the file content has to be moved, to make room for the new data.
要插入文件的中间,必须移动文件内容的其余部分,以便为新数据腾出空间。
You have to do such a move. There is no built-in API for that, not even in C/C++.
你必须做这样的举动。没有内置的API,即使在C / C ++中也没有。
The data files of a database are complex, and even they don't insert new data in the middle of a file.
数据库的数据文件很复杂,甚至不会在文件中间插入新数据。
#1
2
There is no simple way to "insert" a line in the middle of a file. Filesystems and I/O subsystems don't work that way. To truly insert a line you have to copy the file and add the line in the right place as you are copying.
没有简单的方法可以在文件中间“插入”一行。文件系统和I / O子系统不能以这种方式工作。要真正插入一行,您必须复制文件,并在复制时将行添加到正确的位置。
You say "...many database have store the data into the file ..." -- this is true, but they do it with sophisticated block-level techniques where they maintain chains of blocks on disk and update pointers to make it look like the rows are inserted. A lot of work goes into making all this transparent to the database user.
你说“......许多数据库都将数据存储到文件中......” - 这是事实,但是他们使用复杂的块级技术来实现这一点,它们在磁盘上维护块链并更新指针以使其看起来像就像插入行一样。许多工作都是为了让所有这些对数据库用户透明。
Writing even a simple "database" that can insert data in the middle of a file is a significant amount of work.
甚至编写一个可以在文件中间插入数据的简单“数据库”也是一项重要的工作。
#2
1
You cannot insert in the middle of a file. C/C++ cannot do that either.
您无法在文件中间插入。 C / C ++也不能这样做。
To insert in the middle of a file, the rest of the file content has to be moved, to make room for the new data.
要插入文件的中间,必须移动文件内容的其余部分,以便为新数据腾出空间。
You have to do such a move. There is no built-in API for that, not even in C/C++.
你必须做这样的举动。没有内置的API,即使在C / C ++中也没有。
The data files of a database are complex, and even they don't insert new data in the middle of a file.
数据库的数据文件很复杂,甚至不会在文件中间插入新数据。