【问题标题】:File not found error for .txt file but not .m3u?.txt 文件但不是 .m3u 的文件未找到错误?
【发布时间】:2012-10-13 18:05:49
【问题描述】:

我正在测试一些我正在为一个学校项目编写的 java 代码,我无法创建一个代表 txt 文件的新文件,但能够为 .m3u 文件创建一个。例如,以下会引发 FileNotFound 异常:

java.io.FileInputStream inputFile;
    try
    {
        File file = new File("consoledata1.txt");
        inputFile = new FileInputStream( file );
        MixTapeConsole mtc = new MixTapeConsole(inputFile);
    }
    catch( Exception ex)
    {
        System.out.println("Could not find file");
        fail("File not found.");
    }

但这不是:

MixTapeModel mtm = new MixTapeModel();
mtm.loadM3U( new java.io.File("MilesDavis.m3u"));

这两个文件都在文件目录中,并确认(通过打印绝对路径)java 正在寻找两者的正确位置。任何想法将不胜感激。

【问题讨论】:

  • 您是否尝试在打开控制台数据的同一块中打开 m3u 文件?只需交换文件名,看看会发生什么。
  • 文件名的大小写是否正确?

标签: java file exception io fileinputstream


【解决方案1】:

获取文件的绝对路径并不意味着它存在!例如

来源

import java.io.File;

public class PathTest {

    public static void main(String[] args) throws Exception {
        File file = new File("DoesNotExist.txt");
        System.out.println(file.getAbsolutePath());
        System.out.println(file.exists());
    }
}

输出

I:\projects\eclipse\Test\DoesNotExist.txt
false

结论

文件不在您认为的位置,或者拼写不正确。

【讨论】:

    【解决方案2】:

    试试 文件 file = new File("someFileName.txt"); file.createNewFile()

    检查 someFileName.txt 文件是否在您的文件夹中或其他位置。

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多