3

I want to read a file from a relative path. I've tried the following code

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

Error:

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

I also tried to specify the path as "Audio/w1.wav", "Audio//w1.wav" but it does not work.

How can I get the system to find the file?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • `AudioStream swar=new AudioStream(in);` What the heck is an `AudioStream`? Use an `AudioInputStream` instead. it can take input from any stream, including one from an URL. – Andrew Thompson Dec 15 '18 at 02:35
  • General tips: 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Dec 15 '18 at 02:36

2 Answers2

3

The problem seems to be a wrong path. To track that down, start with figuring out where . is. To do so run:

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

This should print the entire path you're in, beginning with c:\ or / depending on your os.

Now take a look at that folder in the explorer, is everything you expect to be in . in that folder?

  • If yes: check for typos and switch between \ and / and check file access permissions?
  • If no: adjust your path or move the files.
Poohl
  • 1,932
  • 1
  • 9
  • 22
0

You can so something like this to get the file,

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());
Shivaraj
  • 400
  • 5
  • 16