写一程序,在d:创建一个文件“test.txt”,并向文件输入“HelloWorld”,然后删除文件。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class file {
public static void main(String[] args) {
File file = new File("d:\\", "file.txt");
try {
if(file.exists()){
file.delete();
System.out.println("delete file exists");
}
if(!file.exists()){
file.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write("HelloWorld!");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
file.delete();
}
}