0

我有一个文本文件,其中包含一系列从 AD 开始的 4194304 个字母(4 MB)。我如何随机指向一个字符并将以下一组字符替换为另一个 100 个字符长的文件并将其写入文件?我实际上目前能够做到这一点,但是当我多次迭代它时,我觉得它真的效率很低。

这是我上面提到的一个例子: 链接到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
}
4

2 回答 2

1

您可能想要使用 Java 的随机访问文件功能。Sun/Oracle 有一个Random Access Files 教程,可能对您有用。

如果您不能使用 Java 7,请查看RandomAccessFile,它也具有搜索功能并且自 Java 1.0 以来就存在。

于 2012-05-20T00:58:29.087 回答
-1

首先,对于您的文件,您可以将文件作为全局变量。这将使您在需要时使用该文件而无需再次阅读它。另请注意,如果您继续制作新文件,那么您将丢失已获取的数据。

例如:

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;
}

希望这可以帮助!

于 2012-05-20T00:59:17.557 回答