Hoe to use the File class
i have the follwing directory structure:
C:\JavaApps\src\soundapp\AudioPlayer.java <=== the only source file
C:\JavaApps\resources\classes <=== AudioPlayer.class goes here
C:\JavaApps\esources\scripts\compile.bat <=== compiles AudioPlayer.java
C:\JavaApps\esources\scripts\run.bat <=== launches AudioPlayer with argument C:\audiofiles\ passed to main(). C:\audiofiles\ contains Song.mp3
CASE 1: launch the application via run.bat (see exception below)
got exception when the application is launched by running run.bat:
filename == C:\audiofiles\Song.mp3
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at AudioPlayer.play(Unknown Source)
at AudioPlayer.main(Unknown Source)
CASE 2: launch application from theclass directory (works everytime):
C:\JavaApps\resources\classes>java AudioPlayer C:\audiofiles\
PLEASE EXPLAIN (i want to be able to launch the application from a directory different from that which contains theclass file).
--> code simplifiedfor brevity
class AudioPlayer
{
publicstaticvoid main(String args[])
{
File directory =new File(args[0]);
String[] files = directory.list();
for(int i = 0; i < files.length; i++)
{
String audioFilename = directory.getPath() +"\\" + files[i];
System.out.println("filename == " + audioFilename);
play(audioFilename);
}
}
privatestaticvoid play(String audioFilename)
{
AudioInputStream in =null;
File audioFile =new File(audioFilename);
try
{
in = AudioSystem.getAudioInputStream(audioFile);
}
// EXCEPTION OCCURS ONLY WHEN APPLICATION IS LAUNCHED
// BY run.bat (CASE 1)
catch(UnsupportedAudioFileException uafx)
{
uafx.printStackTrace();
System.exit(1);
}
catch(IOException iox)
{
iox.printStackTrace();
System.exit(1);
}
// code to play audio files (omitted for brevity).
// it works if execution reaches here
}
}

