0

This program is almost done. But the problem is when I edit and put it in a temp file, both the edited and unedited values are written. So, the temp file gets another value (which is the replacement). I would also like to know if there are ways on how I could rename the temp file. I tried looking at other questions' answers but none of those solved my problem.

import java.io.*;
import javax.swing.*;
import java.util.*;

public class SecondChild4 extends SecondParent
{

    public void editFile(String sFileName, String sFileName2)
    {
        try
        {
            sFileName = "Second.csv";
            sFileName2 = "Second.txt";
            File nfile1 = new File("Second.csv");
            File nfile2 = new File("Second.txt");
            File file1 = new File("TempSecond.csv");
            File file2 = new File("TempSecond.txt");

            FileReader reader = new FileReader(sFileName);
            FileReader reader2 = new FileReader(sFileName2);
            BufferedReader br1 = new BufferedReader(reader);
            BufferedReader br2 = new BufferedReader(reader2);
            FileWriter twriter = new FileWriter(file1);
            FileWriter twriter2 = new FileWriter(file2);
            BufferedWriter tbw1 = new BufferedWriter(twriter);
            BufferedWriter tbw2 = new BufferedWriter(twriter2);

            String edit = "";
            String edit2 = "";
            String data = "";
            Scanner scanner = new Scanner(nfile2);
            String _btitle = JOptionPane.showInputDialog (null, "Title: ", "");
            String _bauthor = JOptionPane.showInputDialog (null, "Author: ", "");

                while(scanner.hasNext()){
                    boolean replace = false;
                    String str = scanner.nextLine();

                        if((str.contains(_btitle))&&(str.contains(_bauthor)))
                        {   
                            String conv = str.toString();
                            System.out.println("Search found");
                            String btitle1 = JOptionPane.showInputDialog (null, "Replace with title: ", "");
                            String bauthor1 = JOptionPane.showInputDialog (null, "Replace with author: ", "");
                            edit = str.replaceAll(_btitle, btitle1);
                            edit2 = str.replaceAll(_bauthor, bauthor1);     
                            tbw1.append(edit);
                            tbw1.append(",");
                            tbw1.append(edit2);
                            tbw1.append("\n");
                            tbw2.write(edit);
                            tbw2.write("\t");
                            tbw2.write(edit2);
                            tbw2.newLine();
                            replace = true;
                            //System.out.println("" +edit + "" +edit2); Test output
                        }

                        else {
                            //System.out.println("" +str); Test  output
                            tbw1.append(str);
                            tbw1.append("\n");
                            tbw2.write(str);
                            tbw2.newLine();
                        }
                }
                            tbw1.close();
                            tbw2.close();
                            br1.close();
                            br2.close();
                            nfile1.delete();
                            file1.renameTo(nfile1);
                            nfile2.delete();
                            file2.renameTo(nfile2);
            }

        catch(IOException e)
        {
             e.printStackTrace();
        } 
    }
}

EDIT 1:

Okay. Figured out how to delete and rename. Problem is I have to edit 2 times before it's finally deleted and renamed. (I removed the author because there are problems with the output).

import java.io.*;
import javax.swing.*;
import java.util.*;

public class SecondChild4 extends SecondParent
{
    public void editFile(String sFileName, String sFileName2)
    {
        try
        {
            sFileName = "Second.csv";
            sFileName2 = "Second.txt";
            File nfile1 = new File("Second.csv");
            File nfile2 = new File("Second.txt");
            File file1 = new File("TempSecond.csv");
            File file2 = new File("TempSecond.txt");

            FileReader reader = new FileReader(sFileName);
            FileReader reader2 = new FileReader(sFileName2);
            BufferedReader br1 = new BufferedReader(reader);
            BufferedReader br2 = new BufferedReader(reader2);
            FileWriter twriter = new FileWriter(file1);
            FileWriter twriter2 = new FileWriter(file2);
            BufferedWriter tbw1 = new BufferedWriter(twriter);
            BufferedWriter tbw2 = new BufferedWriter(twriter2);

            String line = "";
            String _btitle = JOptionPane.showInputDialog (null, "Title: ", "");

                while((line = br2.readLine()) !=null){
                    String btitle1;

                        if(line.contains(_btitle))
                        {   
                            String conv = line.toString();
                            System.out.println("Search found");
                            btitle1 = JOptionPane.showInputDialog (null, "Replace with title: ", "");                       
                            tbw1.append(line.replaceAll("" +_btitle, "" +btitle1));
                            tbw1.append("\n");
                            tbw2.write(line.replaceAll("" +_btitle, "" +btitle1));
                            tbw2.newLine(); 
                        }
                        else {
                            tbw1.append(line);
                            tbw1.append("\n");
                            tbw2.write(line);
                            tbw2.newLine();
                        }
                }           
                            twriter.flush();
                            twriter2.flush();
                            tbw1.close();
                            tbw2.close();
                            br1.close();
                            br2.close();
                            nfile1.delete();
                            file1.renameTo(nfile1);
                            nfile2.delete();
                            file2.renameTo(nfile2);
            }

        catch(IOException e)
        {
             e.printStackTrace();
        } 
    }
}
  • possible duplicate of [How to find out why renameTo() failed?](http://stackoverflow.com/questions/1325388/how-to-find-out-why-renameto-failed) – JFPicard Aug 03 '15 at 13:26
  • don't rename, make a temporal copy of the file and open it in a sepparated buffer... – Jordi Castilla Aug 03 '15 at 13:28
  • @JordiCastilla why is that so? – Friency Fernandez Aug 03 '15 at 13:30
  • I think, if you could create a runnable copy of your program with exact requirement and problem, that would be much helpful. – Kartic Aug 03 '15 at 14:11
  • because JVS keeps references, not object or variable names. If you use same buffer for 2 file locations JVM will threat them as one single buffer, then changes will affect both files... you see strange because you try to make a copy, but try it with a different file and you will see how it gets all overwritted – Jordi Castilla Aug 03 '15 at 15:03
  • @Kartic I tried identifying the problem and its seems that the string I'm trying to replace isn't replaced at all, but just appended. – Friency Fernandez Aug 04 '15 at 02:57
  • @JordiCastilla Got it. But there is still a problem (as stated in the new edit) – Friency Fernandez Aug 04 '15 at 04:31

0 Answers0