0
我想刪除用於閱讀的源文件。但刪除部分不起作用。我使用的代碼,但是當我編譯它跳到else部分嘗試刪除源文件/閱讀文件後讀取它並寫入新文件
public class delete{
public static void main (String[] args){
BufferedReader br=null;
BufferedWriter bw= null;
String outFileName= "C:\\Users\\dokania\\Desktop\\New folder\\out.txt";
File file1 = new File("C:\\Users\\dokania\\Desktop\\New folder\\casp10.txt");
try{
String s;
int fileCounter=0;
FileWriter fw = new FileWriter(outFileName);
bw = new BufferedWriter(fw);
br=new BufferedReader(new FileReader(file1));
while ((s=br.readLine())!= null){
bw.write(s +"\n");
}
boolean success = (new File (file1.getName())).delete();
if (success)
{
System.out.println(file1.getName()+ "file isdeleted");
}
else
{
System.out.println(file1.getName()+ "file not deleted");
}
}catch(IOException e) {
e.printStackTrace();
}
finally{
try{
if(br!=null){
br.close();
bw.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
感謝您的幫助。在我的代碼中,我忘記了在刪除操作之前關閉我的閱讀文件。上面的代碼和評論幫助我理解了我的缺點。 – Unknown