当前位置: 代码迷 >> J2SE >> java io 的有关问题,请
  详细解决方案

java io 的有关问题,请

热度:557   发布时间:2016-04-24 02:20:50.0
java io 的问题,请高手指点
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();        }    }}
  相关解决方案