1

So I'm trying to delete a line of data from a file, which I have successfully done by opening a new file and writing all the information that doesn't match with the data that I would like to remove. The problem is, after I have done that, I would like to delete my original file, and then rename the new file with excludes the information I wanted to delete, to the same name as the original file. I have added in the code to do this, but for some reason it's not working.

public static void delete() throws IOException
{
    File inputFile = new File("Elements.txt");
    File tempFile = new File("myTempFile.txt");

    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

    String element = JOptionPane.showInputDialog(null, "Enter the name of the Element you wish to delete.", "Remove an Element.", JOptionPane.INFORMATION_MESSAGE);;
    String currentLine;

    while((currentLine = reader.readLine()) != null) {
        String trimmedLine = currentLine.trim();
        if(trimmedLine.startsWith(element)) continue;
        writer.write(currentLine + System.getProperty("line.separator"));
    }
    writer.close(); 
    reader.close(); 

    inputFile.delete();
    tempFile.renameTo(inputFile);

    JOptionPane.showMessageDialog(null, "Data has been removed from the file: Elements.txt");
}

As you can see near the bottom, I have these lines:

inputFile.delete();

tempFile.renameTo(inputFile);

These lines are meant to delete my original file(inputFile) and then rename my new file(tempFile) to the file name that the original file had. After running the code however, I simply get a file called "myTempFile.txt" which has succesfully deleted the line of data that I wanted, but my original file is still present and it wasn't deleted, neither was the new file renamed to the original file.

Any idea why this is happening?

W. Ahmed
  • 133
  • 1
  • 4
  • 14
  • 2
    Are you checking the IO exception somewhere, or simply discarding it? My guess is that the delete is failing and throwing an exception. – Alan Jan 10 '15 at 22:03

3 Answers3

3

Use the java.nio.file API. This is 2015.

final Path src = Paths.get("Elements.txt").toAbsolutePath();
final Path tmp = src.resolveSibling("Elements.txt.new");

try (
    final BufferedReader reader = Files.newBufferedReader(src, StandardCharsets.UTF_8);
    final BufferedWriter writer = Files.newBufferedWriter(tmp, StandardCharsets.UTF_8,
        StandardOpenOption.CREATE_NEW);
) {
    // yadda yadda
}

Files.move(tmp, src, StandardCopyOption.REPLACE_EXISTING);

File is unreliable. It has always been.

fge
  • 119,121
  • 33
  • 254
  • 329
  • That's `java.nio.file.Path`. You do use Java 7+, right? – fge Jan 10 '15 at 22:45
  • Tells me 'try' without 'catch' for when you open the reader and writer, and yes I have Java 7+ and found the class to import – W. Ahmed Jan 10 '15 at 22:48
  • Are you sure you copy the _exact syntax I use_? Note that this is a pair of _parentheses_ right after `try`, not a pair of brackets! – fge Jan 10 '15 at 22:56
  • So i just decided to run it without the try on the reader and write, and I get this error message: java.nio.file.AccessDeniedException: null (in sun.nio.fs.WindowsException), showing up on this line: Files.move(tmp, src, StandardCopyOption.REPLACE_EXISTING, – W. Ahmed Jan 10 '15 at 22:59
  • My guess is that you failed to close the reader and writer. This is why you MUST use a try-with-resources statement as I do in the code. It will ensure that both are closed. – fge Jan 10 '15 at 23:01
  • I see, but what resources do you put in the try on the reader on writer? – W. Ahmed Jan 10 '15 at 23:03
  • I don't understand your question... The resources here are the reader and the writer themselves. See [here](http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). – fge Jan 10 '15 at 23:12
  • I mean when i put the try on the reader and writer, it tells me 'try' without 'catch', so what do i put for it to catch? – W. Ahmed Jan 10 '15 at 23:14
  • Wait, did you read what I said above? Right after the `try` it is a pair of parens (`()`), not brackets (`{}`). The code compiles as it is written. – fge Jan 10 '15 at 23:16
  • Oh, I tried this at first but it looked really awkward, like this: http://i.imgur.com/BdGdb1s.png , so I assumed it was a typo and turned it into brackets. – W. Ahmed Jan 10 '15 at 23:21
  • Well, no, those are parens. And you need not close by hand. The try-with-resources does that itself. – fge Jan 10 '15 at 23:33
  • I see, I went ahead and did that, but I am lost as to what i'm doing wrong here: http://i.imgur.com/Fr95A3t.png – W. Ahmed Jan 10 '15 at 23:56
  • 1. you are missing the closing paren before the opening bracket; 2. remove the lines `writer.close()` and `reader.close()` after the try block. Just like my code does in fact. – fge Jan 10 '15 at 23:57
  • OK, well, try and remove the `Files.move()` with `StandardCopyOption.ATOMIC_MOVE`. After that, if you still get this error, you have a permission problem. – fge Jan 11 '15 at 00:36
  • Do you mean replace? After doing this, here's what I get: http://i.imgur.com/HClyD5b.png , I've already imported java.nio.file.StandardCopyOption into my program so not sure why this is coming up – W. Ahmed Jan 11 '15 at 00:42
  • I didn't mean this at all... I meant to replace the last try/catch with only `Files.move(tmp, src, StandardCopyOption.REPLACE_EXISTING)`. – fge Jan 11 '15 at 00:54
  • OK, see post edit. But you should have figured out what I meant. Please consult the javadoc for the classes I use. – fge Jan 11 '15 at 00:55
  • Ah I see, i altered the code and now it gives me a FileSystemsException and says it cannot access the file because it is in use by another proccess, I don't have the txt file open in the background – W. Ahmed Jan 11 '15 at 01:08
  • Are you sure it's not your application itself which has a handle on it in some other place? – fge Jan 11 '15 at 01:44
0

in such a case i would start fiddling around, reading documentation and maybe googling for a bit. But i will give you an answer, too!

inputFile.delete();

This could go wrong, for example if you have your file opened in a text editor. Luckily delete() returns a boolean, try checking that!

Also as Niels correctly mentioned File.renameTo() is quite unrelieble if you have access to Java 7 use the files.nio alternative. In Java 7 you can use Files.move(Path source, Path target, CopyOption... options)

Docs for Java 7 Files: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

NooBxGockeL
  • 103
  • 1
  • 7
0

But your very code works correctly for me. I only change the path to the file and I make sure the file is not opened in editor

public class NewClass {

public static void main(String[] args) {
    try {
        delete();
    } catch (IOException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void delete() throws IOException {
    File inputFile = new File("C:\\Users\\olyjosh\\Desktop\\Elements.txt");
    File tempFile = new File("C:\\Users\\olyjosh\\Desktop\\myTempFile.txt");

    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

    String element = JOptionPane.showInputDialog(null, "Enter the name of the Element you wish to delete.", "Remove an Element.", JOptionPane.INFORMATION_MESSAGE);;
    String currentLine;

    while ((currentLine = reader.readLine()) != null) {
        String trimmedLine = currentLine.trim();
        if (trimmedLine.startsWith(element)) {
            continue;
        }
        writer.write(currentLine + System.getProperty("line.separator"));
    }
    writer.close();
    reader.close();

    inputFile.delete();
    tempFile.renameTo(inputFile);

    JOptionPane.showMessageDialog(null, "Data has been removed from the file: Elements.txt");
}

}
olyjosh
  • 431
  • 7
  • 15
  • Hey, what exactly do you mean by changing the path? I tried your suggestion by putting the try and catch statement at the start of my procedure, and I get a message saying cannot find symbol - class NewClass – W. Ahmed Jan 13 '15 at 01:38
  • The path where those files where located - In my own case, it's on my windows desktop directory. As you can see "NewClass" is the name of the class I use in executing you r code.So you should change the "NewClass" the name of your class where you have this very method defined. – olyjosh Jan 18 '15 at 14:12