3 Replies Latest reply on Aug 7, 2011 10:30 AM by EJP

    f.delete() and f.renameTo() does not work

    803787
      Hi,

      I have the following code, where I am trying to delete a file and renaming another one. The f.exists() is returning true and the files truly exist, but f.delete() and f.renameTo() are simply not working. Any thoughts? Thanks

      code_tags
      try{
      Scanner input = new Scanner(new FileReader(flight+".txt"));
      FileWriter fstream = new FileWriter(flight+"B.txt",true);
      BufferedWriter out = new BufferedWriter(fstream);
      String strLine = input.nextLine();

      while(strLine!= null && (strLine.compareTo(seatlabel.getText())<0)) {


      try {
      for(int i =0; i<5; i++){
      out.write(strLine);
      out.newLine();
      strLine = input.nextLine();
      }
      }catch(java.util.NoSuchElementException e){
      strLine = null;
      }
      }

      out.write(seatlabel.getText());
      out.newLine();
      out.write(clalabel.getText());
      out.newLine();
      out.write(namelabel.getText()+ " " +name.getText());
      out.newLine();
      out.write(dobLabel.getText()+ " " +dob.getText());
      out.newLine();
      out.write(idlabel.getText()+ " " +ID.getText());
      out.newLine();
      out.write(natlabel.getText()+ " "+nat.getText());
      out.newLine();
      out.write("--------");
      out.newLine();

      while(strLine!=null){
      try {

      out.write(strLine);
      out.newLine();
      strLine = input.nextLine();
      }catch(java.util.NoSuchElementException e){
      strLine = null;
      }
      }


      input.close();
      out.close();
      fstream.close();

      File f = new File (flight+".txt");
      if(f.exists()){
      boolean ans = f.delete();
      System.out.println(ans);
      }
      File f2 = new File (flight+"B.txt");
      if(f2.exists()){
      f2.renameTo(new File(flight+".txt"));


      }


      }catch (Exception e){
      System.err.println("Error: " + e.getMessage());

      }
      code_tags