0

I am having trouble editing a record in a text file in Java. I am able to print to the temporary file, but am unable to delete the main file and rename the temporary file.

private void editFile(String old, String newLine) throws FileNotFoundException, IOException {
    try {  
        File inFile = new File ("Members.txt"); 
        File tempFile = new File ("Members_Temp.txt");
        inFile.setReadable(true);
        inFile.setWritable(true);
        tempFile.setReadable(true);
        tempFile.setWritable(true);
        PrintWriter PW;

        try ( 
            //Defines the BufferedReader to read the Current File
            BufferedReader BR = new BufferedReader(new FileReader(inFile))) {
            FileWriter temp = new FileWriter(tempFile);
            PW = new PrintWriter (temp);
            String line = null;
            //While Loop to read the current file till it ends
            while ((line = BR.readLine()) != null) {
                String replace = old.replace(old, newLine); //Replaces old data with the new data
                PW.write(replace); //Writes to the temporary file
                //PW.flush();
            }

            BR.close();
            PW.close();

            inFile.delete();
            tempFile.renameTo(inFile);
        }            
    }
    catch (IOException ex) {          
    }

}

I have tried swapping the statements around but that doesn't see to work either. I was considering the Files.move statement but am unsure on how to use it? The files are found within the program folder, and I plan on making it portable so the directories will be different when the program is used on a different computer, hence why I thought the paths would be different each time.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
JSmithers
  • 83
  • 8
  • You appear to be removing all the newlines from the file. I would never ignore an exception especially in code which is not doing what I think it should be. Are you sure you are not getting an error before the code to delete/rename is called? – Peter Lawrey Feb 23 '16 at 18:37
  • 1
    Never shallow exceptions: you have written `catch (IOException ex) { /*NEVER nothing here*/ }`. Change it to `catch (IOException ex) { ex.printStackTrace(); }` and you will know a lot more. – zloster Feb 23 '16 at 18:41
  • @JSmithers do add a log in the catch block. you may want to check whether the file was actually deleted or not. – chaitanya Feb 23 '16 at 18:42
  • I assume you meant `line.replace(old, newLine)`, because your current code simply writes `newLine` to the output X number of times. – Andreas Feb 23 '16 at 18:43
  • It would be great if you put problem statement here !! – Ravindra Devadiga Feb 23 '16 at 18:58
  • I added a catch block now: catch (IOException ex) { ex.printStackTrace(); } However it still doesn't seem to be renaming or deleting the file. I also changed my code to line. replace(). – JSmithers Feb 24 '16 at 05:01
  • @RavindraDevadiga the aim of the program is to be able to add, edit, delete and search member records stored in a text file. My add and search functions work but my edit and delete ones don't. – JSmithers Feb 24 '16 at 07:32

1 Answers1

0

Below code works completely fine for me. It edits the file as well as deletes file.

private void editFile(String old, String newLine)
            throws FileNotFoundException, IOException {
        try {
            File inFile = new File("Members.txt");
            File tempFile = new File("Members_Temp.txt");
            inFile.setReadable(true);
            inFile.setWritable(true);
            tempFile.setReadable(true);
            tempFile.setWritable(true);
            PrintWriter PW;

            try (
            // Defines the BufferedReader to read the Current File
            BufferedReader BR = new BufferedReader(new FileReader(inFile))) {
                FileWriter temp = new FileWriter(tempFile);
                PW = new PrintWriter(temp);
                PW.write("");
                String line = null;
                // While Loop to read the current file till it ends
                while ((line = BR.readLine()) != null) {
                    if (line.contains(old)) {
                        String replace = line.replace(old, newLine);
                        PW.append(replace+"\n");
                    } else

                        PW.append(line+"\n");
                }

                BR.close();
                PW.close();

                inFile.delete();
                tempFile.renameTo(inFile);


            }
        } catch (IOException ex) {
        }

    }

How to test?

Initially Members.txt file will have following entry

ravi
ravindra
devadiga

And parameter to edit method will be "indra" and "dee"

After you run the java application, Members.txt will have following entry

ravi
ravdee
devadiga

Means, Members_Temp.txt file got created and renamed to Members.txt after deleting original Members.txt

Ravindra Devadiga
  • 692
  • 1
  • 6
  • 14