Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:23:13+00:00 2026-06-04T04:23:13+00:00

I have a text file with a sequence of 4194304 letters ranging from A-D

  • 0

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.

Here’s an illustration of what I mentioned above:
Link to Imageshack

Here’s how I’m currently achieving this:

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 {
          ...
    }

Thanks for reading!

EDIT:
For those curious, here’s the code I used to solve the aforementioned problem:

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:23:14+00:00Added an answer on June 4, 2026 at 4:23 am

    You probably want to use Java’s random-access file features. Sun/Oracle has a Random Access Files tutorial that will probably be useful to you.

    If you can’t use Java 7, then look at RandomAccessFile which also has seek functionality and has existed since Java 1.0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three
I have a text file and I would like to parse it using regular
I have a text file exported from a Foxpro (Dos-based) program, but this text
I want to append two text files together. I have one file with a
I have a text file that contains protein sequences. I would like to get
I have a plain text-file (.yml) that contains UTF-8 character sequences like this: foo:
I have text file (seq.fasta) which contains sequence as follows M1 MPMILGYWNVRGLTHPIRMLLEYTDSSYDEKRYTMGDAPDFDRSQWLNEKFKLGLDFPNL PYLIDGSHKITQSNAILRYLARKHHLDGETEEERIRADIVENQVMDTRMQLIMLCYNPDF EKQKPEFLKTIPEKMKLYSEFLGKRPWFAGDKVTYVDFLAYDILDQYRMFEPKCLDAFPN
I have Text file that contains data separated with a comma , . How
I have text file, like FILED AS OF DATE: 20090209 DATE AS OF CHANGE:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.