编写一个简单的java程序

编写一个简单的java程序,打开一个文件,在文件中写入字符串,然后删除文件。

第1个回答  2013-11-05
public class Hello {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
Hello hello = new Hello();
String path = "D:/abc.txt";
// 写入文件
hello.writeFile(path, "Hello world !");

// 删除文件
//hello.deleteFile(path);
}

/**
 * 文件写入
 * @param path 文件路径
 * @param pValue 写入内容
 * @throws IOException IO异常
 */
private void writeFile(String path, String pValue) throws IOException{
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
fos.write(pValue.getBytes());
fos.flush();
fos.close();
}

/**
 * 根据路径删除文件
 * @param path 文件路径
 * @throws IOException IO异常
 */
private void deleteFile(String path) throws IOException{
File file = new File(path);
if(file.exists()){
file.delete();
}
}
}

本回答被提问者采纳
第2个回答  2013-11-05
File f=new File("d:/my.txt");
if(!f.exists()){
f.createNewFile();
}
FileWriter out=new FileWriter(f);
out.write("ttttttttt");
out.close();
f.delete();
相似回答