I have an array [a, b, c, d] that I wish to save to a .txt file. My code allows a user to edit a specific line of text file as it deletes the rest of the files contents on edit I have decided to save the contents of a file into an array. I then want to save the contents of the array back into the text file.
import java.io.*; import java.util.*; public class LineNumberReaderExample { public static void main(String[] args) throws Exception { boolean foundIt = false; Scanner input=new Scanner(System.in); System.out.print("Enter record to edit "); String word=input.next(); File f=new File("file.txt"); BufferedReader freader; freader = new BufferedReader(new FileReader(f)); String s; List<String> list = new ArrayList<String>(); while((s = freader.readLine()) != null) { list.add(s); System.out.print(list); if(s.startsWith(word)){ while(foundIt == false) { System.out.print(s); foundIt = true; System.out.print("Replace With: "); String newtext=input.next(); FileWriter writer = new FileWriter("file1.txt"); writer.write(s.replace(s, newtext)); writer.close(); list.add(newtext); } } System.out.println(list); // NEED CODE TO SAVE ARRAY TO FILE // } } }