2013-10-16 53 views
0

我想修改文件中的數據。同時檢查它是否已成功刪除並重命名文件,它始終打印「未成功更改」。文件重命名和刪除錯誤

public void transfer() 
{ 

    File temp=new File("temp.dat"); 
    File supply=new File("supply.dat"); 
    boolean rename=temp.renameTo(supply); 
    boolean delete=supply.delete(); 

    if(delete && rename) 
     Sopln("Successfully Changed"); 
    else 
     Sopln("Not Successfully Changed"); 
}//transfer end 

這裏是我在調用此方法的switch-case塊。

Sopln("Modify :-"); 
    Sopln("1)Preffered Period"); 
    Sopln("2)Preffered Class"); 
    Sopln("3)Exit"); 
    int option; 
    do 
    { 
     option=Check_Int("Enter Your Choice"); 
     int ctr=0; 
     switch(option) 
     { 
      case 1: Sopln("______________________________"); 
        String name=Check_String("Enter The Name Of The Teacher Whose Details You Want To Change"); 
        String record; 
        while((record=fr.readLine())!=null) 
        { 
         String token[]=record.split("-"); 
         if(name.equalsIgnoreCase(token[0])) 
         { 
          Sopln("Your Current Info:"); 
          Sopln("Name : "+token[0]); 
          Sopln("Preffered Period : "+token[3]); 
          Sopln("Enter Your New Preffered Period"); 
          token[3]=br.readLine(); 
          pw.println(token[0]+"-"+token[1]+"-"+token[2]+"-"+token[3]+"-"+token[4]); 
          ctr++; 
         }//if block end 
         else 
         {pw.println(record);} 
        }//while loop end 
        if(ctr==0) 
        {Sopln("Not Found"); 
        submenu(); 
        } 
        else 
        { 
         ctr=0; 
         transfer(); 
        } 
        fr.close(); 
        pw.close(); 
        break; 
      case 2: Sopln("______________________________"); 
        String name_class=Check_String("Enter The Name Of The Teacher Whose Details You Want To Change"); 
        String record_class; 
        int cnt=0; 
        while((record_class=fr.readLine())!=null) 
        { 
         String token[]=record_class.split("-"); 
         if(name_class.equalsIgnoreCase(token[0])) 
         { 
          Sopln("Your Current Information:-"); 
          Sopln("Name : "+token[0]); 
          Sopln("Preffered Class : "+token[4]); 
          Sopln("Enter Your New Preffered Class"); 
          token[4]=br.readLine(); 
          pw.println(token[0]+"-"+token[1]+"-"+token[2]+"-"+token[3]+"-"+token[4]); 
          cnt++; 
         }//if block end 
         else 
         {pw.println(record_class);}//else block end 
         }//while loop end 
         if(cnt==0) 
         {Sopln("Name Not Found!"); 
         submenu();} 
         else 
         { 
          cnt=0; 
          transfer(); 
         }//else block end 
         fr.close(); 
         pw.close(); 
         break; 
      case 3: main_menu mm=new main_menu(); 
        mm.menu(); 
        break; 
     } 
    }while(option<0 || option>2); 
}//change method end 

所以,當我檢查我的文件,臨時文件與必要的修改,但創造,但該文件沒有被改名,也不是原來的供應文件被刪除。 Plz Help !!

+0

確定文件路徑是正確的? – UDPLover

+0

你是什麼意思?沒有得到你 – DragonBlade

+0

你如何運行這個程序?來自Eclipse或Netbeans IDE,還是來自CMD?這些文件在哪裏? – UDPLover

回答

0

您首先要重命名,然後創建一個新的文件變量&然後刪除。解決方案簡單

public void transfer() 

{ //重命名文件 文件臨時=新文件( 「TEMP.DAT」); File supply = new File(「supply.dat」); boolean rename = temp.renameTo(supply);

//delete file 
File f = new File("supply.dat"); 
boolean delete=supply.delete(); 


if(delete && rename) 
    Sopln("Successfully Changed"); 
else 
    Sopln("Not Successfully Changed"); 

} //傳送結束

+0

沒有,仍然沒有工作:/ – DragonBlade

+0

它不是內部傳輸方法 –

+0

等待!有用!這個類是菜單的一個選項。但如果我選擇菜單中的其他選項,然後選擇'修改'它顯示「未成功更改」。 – DragonBlade