OasisTea
OasisTea

Reputation: 67

Reading file in Java language

Hello I am having trouble reading a file in Java into a bytearray. Can someone help me with properly reading the file as I am getting errors including "The system cannot find the file specified" thought I have saved the file in the src folder.

Upvotes: 1

Views: 205

Answers (3)

영택송
영택송

Reputation: 1

you can use this command:

File fin = new File("111.txt");
FileInputStream fin1 = new FileInputStream(fin);
BufferedInputStream f =new BufferedInputStream(fin1);

Note: if you want to call your file, your file has to be in your package

Upvotes: -1

Mark Peters
Mark Peters

Reputation: 81154

If you don't provide a full path, the filename you specify will be relative to the working directory of your program.

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked. - File javadoc

It is therefore dependent on how you launch your code. Either use an absolute path, or put the file in the correct directory.

Typically if the file you want to read is to be distributed with your code, you instead want to use Java's resource capabilities instead of interacting with the filesystem by putting the file in the classpath and loading it with ClassLoader.getResource().

Upvotes: 2

thedayofcondor
thedayofcondor

Reputation: 3876

You can use the Applet AudioClip api:

http://journals.ecs.soton.ac.uk/java/tutorial/applet/ui/sound.html

 AudioCLip clip=Applet.newAudioClip(wave.class.getResource("auSample.au"));
 clip.play();

if the clip is in the same package as the class. If it is instead on the root, use:

  AudioCLip clip=Applet.newAudioClip(wave.class.getResource("/auSample.au"));

Upvotes: 0

Related Questions