java.io.FileNotFoundException: D:\Test (拒绝访问。)

时间:2022-10-30 08:33:12

解决java.io.FileNotFoundException: D:\Test (拒绝访问。)问题

源码:

public static void main(String[] args)
{
File file = new File("D:\\Test");
try
{
BufferedWriter bf = new BufferedWriter(new FileWriter(file));
bf.write("sdf");
bf.flush();
bf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}




在写文件是报下面异常:
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:145)
at java.io.FileWriter.<init>(FileWriter.java:73)
at com.bonzer.testajax.test.main(test.java:23)
原因在实例化File file=new File(fileAllName);的时候fileAllName是一个目录File file = new File("D:\\Test");
而在下面这就就报上面的异常了。
FileWriter fw=new FileWriter(file);
解决办法,将fileAllName具体到文件名字。File file = new File("D:\\Test\\sdf.txt");