gpt4 book ai didi

javascript - 读取源文件/读取文件并写入新文件后尝试删除它

转载 作者:行者123 更新时间:2023-12-03 09:43:01 25 4
gpt4 key购买 nike

我正在尝试删除用于读取的源文件。但删除部分不起作用。我使用了代码,但是当我编译它时跳到其他部分

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();
}
}

}
}

最佳答案

 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");

}
br.close();

if (file1.delete()) {
System.out.println(file1.getName() + " is deleted!");
} else {
System.out.println("Delete operation is failed.");
}

} catch (IOException e) {

e.printStackTrace();
} finally {

try {

if (br != null) {
br.close();
bw.close();
}
} catch (IOException e) {

e.printStackTrace();
}
}

}

}

关于javascript - 读取源文件/读取文件并写入新文件后尝试删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31130731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com