Java 读取文件并将文本添加到随机字符位置
我有一个文本文件,其中包含从a到D的4194304个字母序列,全部在一行(4MB)上。 如何随机指向一个字符并将以下字符集替换为另一个100个字符长的文件,然后将其写入文件? 我现在确实能够做到这一点,但我觉得迭代几次效率很低 下面是我上面提到的一个例子: 以下是我目前如何实现这一目标:Java 读取文件并将文本添加到随机字符位置,java,file,replace,random-access,Java,File,Replace,Random Access,我有一个文本文件,其中包含从a到D的4194304个字母序列,全部在一行(4MB)上。 如何随机指向一个字符并将以下字符集替换为另一个100个字符长的文件,然后将其写入文件? 我现在确实能够做到这一点,但我觉得迭代几次效率很低 下面是我上面提到的一个例子: 以下是我目前如何实现这一目标: Random rnum = new Random(); FileInputStream fin = null; FileOutputStream fout = null; int count = 10000;
Random rnum = new Random();
FileInputStream fin = null;
FileOutputStream fout = null;
int count = 10000;
FileInputStream fin1 = null;
File file1 = new File("fileWithSet100C.txt");
int randChar = 0;
while(cnt > 0){
try {
int c = 4194304 - 100;
randChar = rnum.nextInt(c);
File file = new File("file.txt");
//seems inefficient to initiate these guys over and over
fin = new FileInputStream(file);
fin1 = new FileInputStream(file1);
//would like to remove this and have it just replace the original
fout = new FileOutputStream("newfile.txt");
int byte_read;
int byte_read2;
byte[] buffer = new byte[randChar];
byte[] buffer2 = new byte[(int)file1.length()]; //4m
byte_read = fin.read(buffer);
byte_read2 = fin1.read(buffer2);
fout.write(buffer, 0, byte_read);
fout.write(buffer2, 0, byte_read2);
byte_read = fin.read(buffer2);
buffer = new byte[4096]; //4m
while((byte_read = (fin.read(buffer))) != -1){
fout.write(buffer, 0, byte_read);
}
cnt--;
}
catch (...) {
...
}
finally {
...
}
try{
File file = new File("newfile.txt");
fin = new FileInputStream(file);
fout = new FileOutputStream("file.txt");
int byte_read;
byte[] buffer = new byte[4096]; //4m
byte_read = fin.read(buffer);
while((byte_read = (fin.read(buffer))) != -1){
fout.write(buffer, 0, byte_read);
}
}
catch (...) {
...
}
finally {
...
}
谢谢你的阅读
编辑:
对于那些好奇的人,以下是我用来解决上述问题的代码:
String stringToInsert = "insertSTringHERE";
byte[] answerByteArray = stringToInsert.getBytes();
ByteBuffer byteBuffer = ByteBuffer.wrap(answerByteArray);
Random rnum = new Random();
randChar = rnum.nextInt(4194002); //4MB worth of bytes
File fi = new File("file.txt");
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(fi, "rw");
} catch (FileNotFoundException e1) {
// TODO error handling and logging
}
FileChannel fo = null;
fo = raf.getChannel();
// Move to the beginning of the file and write out the contents
// of the byteBuffer.
try {
outputFileChannel.position(randChar);
while(byteBuffer.hasRemaining()) {
fo.write(byteBuffer);
}
} catch (IOException e) {
// TODO error handling and logging
}
try {
outputFileChannel.close();
} catch (IOException e) {
// TODO error handling and logging
}
try {
randomAccessFile.close();
} catch (IOException e) {
// TODO error handling and logging
}
您可能想使用Java的随机访问文件特性。Sun/Oracle有一个可能对您有用的解决方案
如果您不能使用Java 7,那么请查看自Java 1.0以来还具有seek功能的文件。首先,对于您的文件,您可以将文件作为全局变量。这将使您在需要时无需再次读取即可使用该文件。还要注意的是,如果您继续制作新文件,那么您将丢失已获取的数据 例如:
public class Foo {
// Gloabal Vars //
File file;
public Foo(String location) {
// Do Something
file = new File(location);
}
public add() {
// Add
}
}
回答你的问题,我会先阅读这两个文件,然后在内存中进行所有你想要的更改。完成所有更改后,我会将更改写入文件
但是,如果文件非常大,那么我会在磁盘上逐个进行所有更改。。。这样会比较慢,但不会耗尽内存。对于你正在做的事情,我怀疑你是否可以使用缓冲区来帮助抵消它会有多慢
我的总体建议是使用数组。例如,我会做以下事情
public char[] addCharsToString(String str, char[] newChars, int index) {
char[] string = str.toCharArray();
char[] tmp = new char[string.length + newChars.length];
System.arraycopy(string, 0, tmp, 0, index);
System.arraycopy(newChars, index, tmp, index, newChars.length);
System.arraycopy(string, index + newChars.length, tmp, index + newChars.length, tmp.length - (newChars.length + index));
return tmp;
}
希望这有帮助 嗨,Quantum,我查过了,但看起来我需要1.7。不幸的是,如果我切换,我会因为不赞成而丢失所有其他导入。你说的“丢失所有其他导入”是什么意思?而且,即使它们被弃用,也不意味着你不能使用它们。对不起。我关于“所有其他导入”的错误。谢谢你的建议,我将查看RandomAccessFile。我对代码进行了更改,它运行得更快!非常感谢你!