Note that there are some explanatory texts on larger screens.

plurals
  1. POReading file and adding text to random character location
    text
    copied!<p>I have a text file with a sequence of 4194304 letters ranging from A-D all on one line (4 MB). How would I randomly point to a character and replace the following set of characters to another file that is 100 characters long and write it out to a file? I'm actually currently able to do this, but I feel it's really inefficient when I iterate it several times.</p> <p>Here's an illustration of what I mentioned above: <a href="http://desmond.imageshack.us/Himg716/scaled.php?server=716&amp;filename=goalw.jpg&amp;res=landing" rel="nofollow">Link to Imageshack</a></p> <p>Here's how I'm currently achieving this:</p> <pre><code>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 &gt; 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 { ... } </code></pre> <p>Thanks for reading!</p> <p>EDIT: For those curious, here's the code I used to solve the aforementioned problem:</p> <pre><code>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 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload