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

}

}

[3373 byte] By [anonimudsa] at [2007-10-3 8:29:30]
# 1
Weird indeed.What I've noticed is that in your second case you have a double \, the one in the command line and the one in the +"\\".Have you tried to use directory.listFiles();Regards,Anthony
agoubarda at 2007-7-15 3:36:22 > top of Java-index,Java Essentials,Java Programming...