delete a file in java does not work
<p>i put this code,which i got from the internet, in my java program but when i try to delete, the original file cannot be deleted and the temporary file cannot be renamed to the original file.The two files remains in the folder with its contents unchanged.</p>

<p>...</p>

<pre><code>public class FilingDatabase {
    public static void main(String[]args)throws IOException{
         (new FilingDatabase()).run();
        FilingDatabase fd=new FilingDatabase();
        String word = null;
        fd.delete("person.txt",word);

       }
</code></pre>

<p>.</p>

<pre><code>public  void run() throws IOException{
    File file=new File("person.txt");
    BufferedReader br=new BufferedReader(new FileReader(file));

    while((str=br.readLine())!=null)
        i++;

       System.out.print("\t\t\t\t\t\t***************WELCOME*****************");
       System.out.println();
       System.out.println("1. Add \n2. Edit \n3. Delete \n4. Exit");
       System.out.print("\nEnter option number: ");
       option=in.next();

        while(true){
            ...
            else if(option.charAt(0)=='3'){
               // FilingDatabase fd= new FilingDatabase();
                System.out.print("Enter word: ");
                word=in.next();
                //delete("person.txt",word);

            }        
           ...
       }
    }//end of fxn run()
</code></pre>

<p>....</p>

<pre><code>public void delete(String file, String lineToRemove) throws IOException{

         try {
                     File inFile = new File(file);
                     if (!inFile.isFile()){
                     System.out.println("File does not exist");
                     return;
                      }
                      File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
                      BufferedReader br = new BufferedReader(new FileReader(file));
                      //Scanner br=new Scanner(file);
                      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

                      String line = null;

                      while ((line = br.readLine()) != null) {
                        if (!line.trim().equals(lineToRemove)) {

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


                      pw.close();
                      br.close();

                      if (!inFile.delete()) {
                        System.out.println("Could not delete file");
                        return;
                      } 
                      if (!tempFile.renameTo(inFile))
                        System.out.println("Could not rename file");

            }catch (FileNotFoundException e) {
                e.printStackTrace();
                }
  }
}
</code></pre>
