[英]Reading file and adding text to random character location
我有一個文本文件,其序列由4194304個字母組成,范圍從AD全部排在一行上(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
}
您可能要使用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;
}
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.