I want to edit a text file. this is the original file:
kopfor20 22 22
second line 3
last line 4
I want it to be like this:
new 22 22 
second line 3
last line 4
What I have done so far is I stored the txt file to an array after that I replaced "kopfor20" with "new" however I can't seem to store the array back to txt file.
Here is what i have done so far :
    public static void main(String[] args) throws IOException {
        File f1 = new File("test.txt");
        if (!f1.exists()) {
            f1.createNewFile();
        }
      FileWriter fw = new FileWriter("test.txt");    
      PrintWriter pw = new PrintWriter("test.txt");
      pw.write("kopfor20 22 22\n");        
      pw.write("second line 3\n");
      pw.write("last line");
      pw.close();
      txttoArray();
      arraytoFile();
      System.out.println("finished");
}
 public static void arraytoFile() {
         try {
         FileWriter fw = new FileWriter("test.txt");
          PrintWriter pw = new PrintWriter("test.txt");
 System.out.println("enter new txt");
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        System.out.println("Enter old txt to replace ");
        String m = sc.next();
        String[] textarray = txttoArray();
        for (int i = 0; i < textarray.length; i++) {
                if (m == textarray[i]) {
                    System.out.println(textarray[i]);
                    textarray[i] = n ;
                   System.out.println(textarray[i]);
                }
            }
        System.out.println("done");
        for (int i = 0; i < textarray.length; i++) {
            pw.write(n);
        }
        pw.close();
 } catch (Exception e) {
     e.printStackTrace();
 }}
 public static String[] txttoArray(){
        try {
            int ctr = 0;
            Scanner sc = new Scanner(new File("test.txt"));
            Scanner s = new Scanner(new File("test.txt"));
            while(sc.hasNext()){
                ctr++;
                sc.next();
            }
            String[] txtline = new String[ctr];
            for (int i = 0; i < ctr; i++) {
                txtline[i] = s.next();
            }
            for (int i = 0; i < txtline.length; i++) {
                System.out.print(txtline[i]+"\n");
        }
    } catch (Exception e) {
        System.out.println("er");
    }
    return null ;
}}
Please see if you can help me regarding this.
 
    