我有一个文本文件,其序列为4194304个字母,范围从A-D全部在一行(4 MB)。 如何随机指向一个字符并将以下字符集替换为另一个长度为100个字符的文件并将其写入文件? 我实际上目前能够做到这一点,但是当我多次迭代它时,我觉得效率非常低。
以下是我上面提到的例子: Link to Imageshack
以下是我目前的实现方式:
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
}
答案 0 :(得分:1)
您可能希望使用Java的随机访问文件功能。 Sun / Oracle有Random Access Files tutorial可能对您有用。
如果您不能使用Java 7,那么请查看RandomAccessFile,它也具有搜索功能,并且自Java 1.0以来就已存在。
答案 1 :(得分:-1)
首先,对于您的文件,您可以将Files作为全局变量。这将是您在需要时使用该文件而无需再次阅读。另请注意,如果您继续制作新文件,那么您将丢失已经获得的数据。
例如:
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;
}
希望这有帮助!