Switching two lines in a text file in Java -


i've been stuck on while , i've decided plead help. have small text file , user wants switch 2 lines it, user enters indexes of 2 lines , have switch em. far ideas have been either use replaceall 2 regexes , a: won't switch them end replacing 1 other, leaving me duplicate , b: have no idea how locate nth line using regex; or use - files.readalllines(paths.get(name)).get(index); 2 lines i'm still struggling actual switching process.

you can use

  • files.readalllines lines list
  • swap 2 elements of list. e.g. collections.swap
  • write lines out update file.

if need able work large files could

  • use randomaccessfile find start/end of lines need reading start of file.
  • read 2 lines buffer.
  • write 2 lines out in place, swapped around.

Comments