读取文件并将文本添加到随机字符位置



我有一个文本文件,其中包含从A到D的4194304字母序列,全部在一行(4 MB(上。我将如何随机指向一个字符并将以下字符集替换为另一个长度为 100 个字符的文件并将其写出到文件中?我目前实际上能够做到这一点,但是当我迭代几次时,我觉得它真的很低效。

以下是我上面提到的说明:链接到图像黑客

以下是我目前实现这一目标的方式:

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,那么看看RandomAccessFile,它也具有查找功能,并且从Java 1.0开始就存在。

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

例如:

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

希望这有帮助!

最新更新