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

     }

最佳答案

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

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

关于java - 将字节数据写入目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308642/

相关文章:

java - 从数据库序列中获取下一个值

java - 如何在 Spring boot、CrudRepository 和 Thymeleaf 中显示具有关系映射的表中的记录

java - 没有找到类 "androidx.core.app.CoreComponentFactory"

java - UserDefinedRepository内部如何通过扩展JpaRepository在Spring Boot中实现方法?

java - Java中String...变量和String变量[]有什么区别?

java - 需要更好的算法来使用 Java 清理 SQL Server 表

java - 获取键盘输入并对其运行检查

java - 我应该使用哪种数据结构来支持键值映射、反向迭代和插入顺序?

java - 如何发送没有正文的 html POST 请求?

java - 我可以在 Struts 2 的隐藏字段中保留对象引用吗?