Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default File Chooser and forward and Backward Slashes

    Hello. I am hoping to write a short application that will play a sound file chosen by the user. I'm using File Chooser so the user can select a sound file to play, and the name and path of the selected sound file is used by the part of the code that plays a sound file. I'm using NetBeans to code this.

    I have the following problem. The File Chooser getSelectedFile() method returns the file using backwards slashes (i.e. C:\User\documents\folder 1\ ..etc.). I think I need the file path in forward slashes for the part of the code that plays the sound file (i.e. C:/User/documents/folder 1/...etc). In a previous verson of this code that did not use the File Chooser, I had hard-coded the sound files using address with forward slashes. This code worked to play the hard-coded sound files.

    In my current version of the code using the File Chooser, I get an error message "fi1: the system cannot find the file specified".

    Any help is appreciated.

    Here is the relevant code from NetBeans
    Java Code:
    private void bPlayActionPerformed(java.awt.event.ActionEvent evt) {                                      
            try {
                // Open an audio input stream
                File sound1 = new File("fi1");
                AudioInputStream audioIn =  AudioSystem.getAudioInputStream(sound1);
                
                // Get a sound clip resource
    	    Clip clip = AudioSystem.getClip();
                
                // Open audio clip and load sample from the audio input stream
                clip.open(audioIn);
    	    clip.loop(3);
            }
            
            catch (UnsupportedAudioFileException uae) {
    	    System.out.println(uae.getMessage());
    	}
            catch (IOException ioe) {
    	    System.out.println(ioe.getMessage());
    	}
            catch (LineUnavailableException lue) {
    	    lue.printStackTrace();
    	}                
        }                                     
    
        private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) {
            // get file name and path
            if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                File fi1 = fileChooser.getSelectedFile();
                String aFi = fi1.getAbsolutePath();
                
                // check the chosen file and path
                System.out.println(fileChooser.getSelectedFile());
                System.out.println("fi1: " + fi1);
                System.out.println("aPath: " + aFi);
                
                
            }
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: File Chooser and forward and Backward Slashes

    printStackTrace() on all your exceptions and find out exactly what error you are getting.
    You do not need to do anything at all to the path returned from the file chooser.
    It's already found a file in that location.

    Except you aren't actually doing anything (useful) with that File.
    It goes out of scope a few lines after you get it.
    It never gets anywhere near the bPlayActionPerformed code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: File Chooser and forward and Backward Slashes

    Hello. Thanks for your reply.

    I added printStackTrace() under each of the three exceptions in the bPlayActionPerformed block (see above). When I ran the program again, there was about 50 lines referenceing java.io.FileNotFoundException and java.io.FileInputStream.

    Sadly, I do not know enough to make use of this information, and correct my problem.

    So I still do not understand how to get the selected file played by the bPlayActionPerfomred block.

    The code after line 29 in the sample above I put in only to help me understand what is being returned by FileChooser. It is not needed for the program to play a osund file so I removed it.

    I noticed that I had omitted the else { statement after line 26 (above), so I added that. Here is the revised fileChooserActionPerfomred code:
    Java Code:
    private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) {
            // get file name and path
            if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                File fi1 = fileChooser.getSelectedFile();
                            
            } else {
                System.out.println("Operation cancelled by user.");
            }
        }
    Thanks for your help.

  4. #4
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: File Chooser and forward and Backward Slashes

    Hello. I just thought of this:

    "It goes out of scope a few lines after you get it.
    It never gets anywhere near the bPlayActionPerformed code. "

    It goes out of scope because fileChooserActionPerformed is private, and the File fi1 is declared within this private method, so that means (I think) bPlayActionPerformed doesn;t know what fi1 is. Is that correct?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default Re: File Chooser and forward and Backward Slashes

    'private' has nothing to do with it.
    'fi1' is a variable local to that method, and when the method exits the object is as good as gone.
    You need to get that reference to the other method, either by storing it as an attribute of whatever class, or possibly by passing it to a method that will open the audio file. Not knowing the structure of your app I can't say which is better.
    Java Code:
    File sound1 = new File("fi1");
    And this bit simply defines a File called "fi1" which, as far as Java is concerned, to be found in the current directory.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. File Chooser Issue
    By mrsethman in forum New To Java
    Replies: 1
    Last Post: 10-19-2012, 09:56 PM
  2. How to manage files in file chooser
    By vaggos in forum New To Java
    Replies: 4
    Last Post: 09-03-2012, 12:09 PM
  3. File chooser internationalization problem
    By Astghik in forum AWT / Swing
    Replies: 10
    Last Post: 01-14-2010, 06:44 PM
  4. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 04:08 AM
  5. Using File Chooser
    By shaungoater in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 12:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •