将字节数据写入目录

时间:2015-02-03 21:00:39

标签: 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 个答案:

答案 0 :(得分:3)

serverFile是一个目录。 FileOutputStream不接受目录。 您无法写入文件等目录 使用类似`

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