0

A program I am writing requires a String to be entered on a specific line of a text file.

The key is that ALL of the lines must be preserved, so if the file looked like:

AAA
BBB
CCC
DDD
FFF

It should look like:

AAA
BBB
CCC
DDD
EEE
FFF 

After the operation. All of the lines shift down to accommodate the new String on the given line. So far I am trying to use a loop, but failing. Any tips would be appreciated.

Code:

// This method does the interfacing with the logfile. Probably don't run it solo.
    // Before: We have a call stored in var ac8ud that needs to be appended to the text file on line 'count' + 1
    // After: The call is inserted and all of the elements below the call are shifted down 1 slot
    //        There will be one more line in the text file than before this was run because of the additional call
    public void writeCall() throws IOException{
    {
    File logpath = new File("log.txt"); 
    List<String> lines = Files.readAllLines(logpath.toPath());
    int count = (call * 10) + 1; // Count is the total amount of lines in the text file.
    String copy2;
    String copy; // Used to hold the next line of the text file in the for loop while it is overwritten 
    String copy1 = lines.get(count - 1); // Copies the last element of list to add on after the loop
    for(int i = call + 1; i < count-1; i++)   {
          copy = lines.get(i); // Read the line that is about to be overwritten by the line above it moving down
          copy2 = lines.get(i + 1); 
          lines.set(i+1, copy); // Move down a line
    }  
    lines.add(copy1);
    lines.set(call + 1, ac8ud.toUpperCase()); // Copy, insert, and overwrite logfile
    Files.write(logpath.toPath(), lines); 
    }
Zeke Legge
  • 17
  • 1
  • 5
  • Please post your code so far. – Andy Turner Jun 28 '15 at 19:15
  • possible duplicate of [I want to open a text file and edit a specific line in java](http://stackoverflow.com/questions/17218971/i-want-to-open-a-text-file-and-edit-a-specific-line-in-java) – Raman Shrivastava Jun 28 '15 at 19:18
  • @RamanShrivastava My program needs to shift lines down in a file, not edit a single line. – Zeke Legge Jun 28 '15 at 19:30
  • @AndyTurner Sure Thing! It isn't complete now though. At this point, it still overwrites instead of inserts. Hopefully, it will be an easy modification. – Zeke Legge Jun 28 '15 at 19:33
  • @Zeke editing a line in the middle of the file is the more general case of inserting a line: consider that you can change a line from "something" to "something\nwith a line break". – Andy Turner Jun 28 '15 at 19:35
  • @AndyTurner That is a good point, but my entire program is built on the fact that the information in the file is separated line by line in the text file, sorta like the .CSV approach, so that won't work for me :D – Zeke Legge Jun 28 '15 at 19:41

1 Answers1

0

If you are reading the contents of a file into a list, inserting a line in the middle of the "file" is simply a matter of calling List.insert(index, value) at the appropriate index.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243