有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将字节数据写入目录

我尝试将字节数据写入目录,我使用以下代码,但我得到了以下结果

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");
  }

     }

共 (1) 个答案

  1. # 1 楼答案

    serverFile是一个目录。FileOutputStream不接受目录。 不能像写入文件那样写入目录
    使用类似于

    File serverFile = new File(dir,"mynewfile.txt");