2am
2am

Reputation: 697

writing to a txt file in random location java

I am trying to write a string to a txt file at an random location, i.e. i want to edit the specific txt file. I do not want to APPEND, but all i want to do is write some string at, say line 3. I tried the same using RandomAccessFile, but when I write to a specific line, it replaces the data at that line.

e.g. a.txt-

1
2
3
4
5

output that is expected by me..

1
2
hi
3
4
5

what i have achieved using RandomAccessFiles

1
2
hi
4
5


I tried to save the content of line 3 before replacing, i used readLine() function for that, and its not working, its giving unexpected results.

Upvotes: 2

Views: 1811

Answers (3)

kentcdodds
kentcdodds

Reputation: 29051

For a file that's not too big for memory:

You could read the file into a String and then do something like this:

String s = "1234567890"; //This is where you'd read the file into a String, but we'll use this for an example.
String randomString = "hi";
char[] chrArry = s.toCharArray(); //get the characters to append to the new string
Random rand = new Random();
int insertSpot = rand.nextInt(chrArry.length + 1); //Get a random spot in the string to insert the random String
//The +1 is to make it so you can append to the end of the string
StringBuilder sb = new StringBuilder();
if (insertSpot == chrArry.length) { //Check whether this should just go to the end of the string.
  sb.append(s).append(randomString);
} else {
  for (int j = 0; j < chrArry.length; j++) {
    if (j == insertSpot) {
      sb.append(randomString);
    }
    sb.append(chrArry[j]);
  }
}
System.out.println(sb.toString());//You could then output the string to the file (override it) here.

For a file too big for memory:

You could do some sort of variation of copying the file where you identify a random spot before-hand and write it in the output stream before the rest of that chunk. Here's some code below:

public static void saveInputStream(InputStream inputStream, File outputFile) throws FileNotFoundException, IOException {
  int size = 4096;
  try (OutputStream out = new FileOutputStream(outputFile)) {
    byte[] buffer = new byte[size];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
      out.write(buffer, 0, length);
      //Have length = the length of the random string and buffer = new byte[size of string]
      //and call out.write(buffer, 0, length) here once in a random spot.
      //Don't forget to reset the buffer = new byte[size] again before the next iteration.
    }
    inputStream.close();
  }
}

Call the above code like this:

InputStream inputStream = new FileInputStream(new File("Your source file.whatever"));
saveInputStream(inputStream, new File("Your output file.whatever"));

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533530

You only have two options, append and over write. There is no insert method for files as they do not support this operations. To insert data, you must re-write the rest of the file to move it down.

Upvotes: 1

user1144031
user1144031

Reputation: 637

There is no API to do that. What you can do is:

  1. Choose a random location
  2. go to the end of the line
  3. Write '\n + 'Your Text'

Upvotes: 1

Related Questions