我尝试将字节数据写入目录,我使用以下代码,但我得到了这个
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");
}
}
最佳答案
serverFile 是一个目录。 FileOutputStream 不接受目录。
您无法像写入文件一样写入目录。
使用类似`
File serverFile = new File(dir,"mynewfile.txt");
关于java - 将字节数据写入目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308642/