Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Newbie, java question - IO Exception

Options
  • 22-11-2004 10:35pm
    #1
    Closed Accounts Posts: 1


    I am just trying to get my simple java program to play a sound when it hits 3 minutes. I found some sample code on how to do this, with a FileInputStream, then send the stream to the audiostream and it should play.Problem is i get an IO Exception - file not found, and I know the file is there. It has to be a syntax issue. Here's my line that generates the error:

    InputStream objInput = new FileInputStream("C:\buzzer_x.wav");

    I also tried this on a whim to no avail, cause I think i remembered that java doesn't like this character when doing paths "\":

    InputStream objInput = new FileInputStream("C:\\buzzer_x.wav");

    Lost on this and it's holding me up, any help would be appreciated. Thank you - Jeremy


Comments

  • Registered Users Posts: 1,865 ✭✭✭Syth


    Are filenames case sensitive in Java? Might wanna check that.

    The '\' caharacter has a speical meaning in Strings, not just filenames. It's called an escape character.


  • Registered Users Posts: 261 ✭✭HaVoC


    import java.applet.Applet;
    import java.applet.AudioClip;
    public class SoundThing {

    public SoundThing(String filename) {


    try {
    AudioClip clip = Applet.newAudioClip( getClass().getResource(filename) );
    clip.play();
    Thread.sleep(600);

    }
    catch (Exception e) {
    System.out.println("Problem with " + filename);
    }
    } // end of SoundThing()






    public static void main(String args[]) {
    String filename = args[0];
    SoundThing sound = new SoundThing(filename);

    }

    }

    This is the easiest way to play a sound clip as fare as I know. audiostream is abit over the top if you just want to play one clip. Its based on .java game dev . For the file name I’m not sure how to an pass absolute path but what I’d do is store your class & java files in one directory and in a sub directory store your sound files ie "myprogram/sound/" then inside the program refer to your files like this “sound/blah.wav” using relative paths.

    Also this way you can move your program to a dfiiferant computer and as long as you copy the directory "myprogram" the filenames wont neeeded to be chanaged inside your code.


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    Well if are going to hardcode a directory into your program at least use a platform independent directory seperator
    String sep = System.getProperty("file.separator");
    that will return the direcory seperator used in the particular OS. So just concat the filename string and use that sep in the concated string to be used as argument to your FileInputStream. I dont like it but it works.


  • Closed Accounts Posts: 92 ✭✭tempest


    patterj wrote:
    InputStream objInput = new FileInputStream("C:\\buzzer_x.wav");

    How about actually debugging your code?
    If you don't have a debugger then how about doing some protective programming and putting in debug printing lines.
    
    File soundFile = new File("C:\\buzzer_x.wav");
    
    if (!soundFile.exists()) {
      System.out.println("The file " + soundFile + " does not exist!");
    } else {
    try {
      InputStream objInput = new FileInputStream(soundFile);
    } catch (IOException e) {
      System.out.println("Error opening: " + soundFile);
      e.printStackTrace();
    }
    }
    

    You might as well learn now as later that you need to be able to solve these kind of problems yourself.

    All the best...
    :)


Advertisement