我正在尝试删除用于阅读的源文件。但删除部分不起作用。我使用了代码但是当我编译它时跳过其他部分
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();
}
}
}
}
答案 0 :(得分:1)
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();
}
}
}
}
答案 1 :(得分:0)
我尝试了你的代码它对我来说非常有用。我想只是尝试刷新你保存文件的项目文件夹,并且只使用单个转义序列来处理文件路径位置