我尝试将字节数据写入目录我使用以下代码但是我得到了这个
Exception in thread "main" java.io.FileNotFoundException: C:\file (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
at com.innvo.domain.App.main(App.java:17)
我的代码:
public static void main(String[] args) throws IOException {
File dir = new File("C:\\file");
if (dir.isDirectory())
{
String x="new text string";
File serverFile = new File(dir.getAbsolutePath());
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
System.out.println(x.getBytes());
stream.close();
}else {
System.out.println("not");
}
}
答案 0 :(得分:3)
serverFile是一个目录。 FileOutputStream不接受目录。 您无法写入文件等目录 使用类似`
的东西File serverFile = new File(dir,"mynewfile.txt");