import java.io.*;
public class TestBufferedWriter {
public static void main(String args[]) {
String name = "弄风尘.txt";
String path = "d:/wowo/";
String s = "我爱北京天安门,天安门上太阳升!";
BufferedWriter bw = null;
try {
File f = new File(path + name);
bw = new BufferedWriter(new FileWriter(f));
bw.write(s);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
控制台输出为
java.io.FileNotFoundException: d:\wowo\弄风尘.txt (系统找不到指定的路径。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at TestBufferedWriter.main(TestBufferedWriter.java:11)
------解决方案--------------------
你这样只是创建文件,不会创建目录,创建目录看下面的程序
- Java code
import java.io.*;public class TestBufferedWriter { public static void main(String args[]) { String name = "弄风尘.txt"; String path = "d:/wowo/"; String s = "我爱北京天安门,天安门上太阳升!"; BufferedWriter bw = null; File folder=new File(path); if(folder.exists()){ if(!folder.isDirectory()){ folder.mkdir(); } } try { File f = new File(path + name); bw = new BufferedWriter(new FileWriter(f)); bw.write(s); bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } }}