使用FileInputStream从相对路径读取File

时间:2018-12-14 20:45:06

标签: java path relative fileinputstream

我想从相对路径读取文件。 我已经尝试了以下代码

InputStream in = new FileInputStream(".//Audio//w1.wav");

错误:

java.io.FileNotFoundException: .\Audio\w1.wav (The system cannot find the path specified)

我还尝试将路径指定为“ Audio / w1.wav”,“ Audio // w1.wav”,但这不起作用。

如何让系统查找文件?

2 个答案:

答案 0 :(得分:0)

您可以这样获取文件,

ClassLoader resource = this.getClass().getClassLoader();
URL path = this.getClass().getResource("/Audio/w1.wav");

//Since you have the path you can get the file as you want
//To get the file,  
File file = new File(path.getFile());

答案 1 :(得分:0)

问题似乎是错误的路径。要找出原因,首先要弄清楚.在哪里。为此,请运行:

System.out.println(new File(".").getAbsolutePath());

这应该打印您所处的整个路径,从c:\/开始,具体取决于您的操作系统。

现在来看一下资源管理器中的该文件夹,您希望该文件夹中的.中的所有内容是吗?

  • 如果是,请检查错别字并在\/之间切换并检查文件访问权限?
  • 如果否:调整路径或移动文件。
相关问题