java - IO异常 : When Executing Java File Code in Linux box

标签 java linux ubuntu

这是我的写文件

WriteFile.writeFile(str, "./test/my.html");

writeFile()方法代码

public static void writeFile(String content, String fileName)
  {
    try
    {
      File file = new File(fileName);
      if (!file.exists()) {
        file.createNewFile();
      }
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(content);
      bw.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

此代码在 Windows 上运行良好,但在 Linux 中我遇到异常

java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1006)
    at org.sewa.util.WriteFile.writeFile(WriteFile.java:25)

最佳答案

createNewFile 的行为在 Windows 和 Linux 中是相同的,因此您指定的文件路径很可能在 Windows 中存在,而在 Linux 中不存在。在您的示例中,test/ 目录在 Linux 中不存在于您执行程序的目录中。如果要创建整个路径,请参阅 File#mkdirs .

关于java - IO异常 : When Executing Java File Code in Linux box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442842/

相关文章:

java - Java音频在定制的Sound类中不断crack啪作响

java - Kafka 流记录在窗口/聚合后不转发

linux - 用于显示的字符存储在哪里?

linux - Linux 中 Qt 动态库的问题

java - 在 Ubuntu Trusty 上安装 openjdk-7-jre 时出错

java - 比较并检查两个字符串中的最后 4 个字符是否相同?

java - Unitils/DBunit/Oracle - 如何在 Oracle View 中插入数据集?

python - 将字符串转换为 bash 转义形式

ubuntu - 安装 MEAN-CLI 时出错

android项目没有建立