0

I am writing a program to find information and remove them from a text file by making a temp file, removing the original one and then renaming the temp to the original file. So far I have achieved to write the program and it works when I compile it using the windows console, but when I try to run the same code in netbeans it does not work because it can't remove and rename the original file. I'm looking for way to solve this problem.

here is code , it works when I compile it using the windows console but not in the netbeans

import java.io.*;

public class rename {
public static String x="1123";

public void removeLineFromFile(String file, String lineToRemove) {

try {

  File inFile = new File(file);

  if (!inFile.isFile()) {
    System.out.println("Parameter is not an existing file");
    return;
  }

  //Construct the new file that will later be renamed to the original filename. 
  File tempFile = new File(inFile.getAbsolutePath() + "2.tmp");

  BufferedReader br = new BufferedReader(new FileReader(file));
  PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

  String line = null;

  //Read from the original file and write to the new 
  //unless content matches data to be removed.
  while ((line = br.readLine()) != null) {

    if (!line.trim().contains(lineToRemove)) {

      pw.println(line);
      pw.flush();
    }
  }
  pw.close();
  br.close();

  //Delete the original file
  if (!inFile.delete()) {
    System.out.println("Could not delete file");
    return;
  } 

  //Rename the new file to the filename the original file had.
  if (!tempFile.renameTo(inFile))
    System.out.println("Could not rename file");

}
catch (FileNotFoundException ex) {
  ex.printStackTrace();
}
catch (IOException ex) {
  ex.printStackTrace();
}
}

public static void main(String[] args) {
rename util = new rename();
String jj;
util.removeLineFromFile("File.txt", x);
}
}
Loop Masters
  • 371
  • 2
  • 5
  • 12
  • What is the error? Post your stack trace. – Anthony Accioly Jun 17 '12 at 14:33
  • Just my custom error : Couldn't remove the file – Loop Masters Jun 17 '12 at 14:34
  • Are you sure the file is not in use by your console application? Or another program? Are all streams closed? Because it works fine for me... – David Kroukamp Jun 17 '12 at 14:38
  • Well, the problem is that is works in console but when I use it with my GUI it doesn't , all files are closed of course. – Loop Masters Jun 17 '12 at 14:39
  • By "running with my GUI" you mean you have wrote a Swing application that uses this method or you are simple running your main method from within Netbeans (Run Main Project / Run File)? – Anthony Accioly Jun 17 '12 at 14:52
  • I am calling this function from a jframe, is this the problem ???? – Loop Masters Jun 17 '12 at 14:56
  • It shouldn't, unless there is another `BufferedReader` or stream pointing to the same file when you call delete (check your code). Another option is that some background thread / process with the old code is still hanging to the file in the background (try closing Netbeans and killing all of your java process, them try again). – Anthony Accioly Jun 17 '12 at 15:01
  • Also make sure that this file isn't open in any other application (such as text editors) by the time you run your program. – Anthony Accioly Jun 17 '12 at 15:09
  • The file is closed, I've checked everything something wrong with my code I think. – Loop Masters Jun 17 '12 at 15:13

2 Answers2

0

Humn... After closing br and pw, try setting them to null and calling System.gc().

Reference: I can't delete a file in java

Community
  • 1
  • 1
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
0

to rename

  public void rename(String old, String newpath) {
    try {
        File folder = new File(old);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {

            if (listOfFiles[i].isFile()) {

                File f = new File(old + listOfFiles[i].getName());

                f.renameTo(new File(old + "\\" + newpath));

                System.out.println(old + "\\" + newpath);
            }
        }

        System.out.println("conversion is done");

    } catch (Exception ex) {
        Logger.getLogger(CVAdd.class.getName()).log(Level.SEVERE, null, ex);
    }

}