FileInputStream not detecting files in java - Eclipse

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 7145 Views - Last Post: 09 November 2011 - 03:57 AM Rate Topic: -----

#1 Norix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 11

FileInputStream not detecting files in java - Eclipse

Posted 06 November 2011 - 03:50 PM

I'm trying to get my code to load an audio file and a text file, but no matter what I do, it keeps saying "The system cannot find the path specified" for these two codes
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Sound extends javax.swing.JFrame {  
  
    /** Creates new form AudioLoopTest */  
    public Sound() {  
        initComponents();  
    }  

     public void playaudio() throws IOException{  
            System.out.println("Welcome in Audio Player");  
            InputStream in = new FileInputStream("music/one.mp3");  
            AudioStream as = new AudioStream(in);  
            
            AudioPlayer.player.start(as);
              
        }  
    
    @SuppressWarnings("unchecked")  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">  
    private void initComponents() {  
  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
        getContentPane().setLayout(layout);  
        layout.setHorizontalGroup(  
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
            .addGap(0, 400, Short.MAX_VALUE)  
        );  
        layout.setVerticalGroup(  
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
            .addGap(0, 300, Short.MAX_VALUE)  
        );  
  
        pack();  
    }// </editor-fold>  
  
    /** 
    * @param args the command line arguments 
    */  
    public static void main(String args[]) {  
        java.awt.EventQueue.invokeLater(new Runnable() {  
            public void run() {  
                new Sound().setVisible(true);  
                try {  
                    new Sound().playaudio();  
                } catch (IOException ex) {  
                    Logger.getLogger(Sound.class.getName()).log(Level.SEVERE, null, ex);  
                    System.out.println(ex.toString());  
                }  
            }  
        });  
    }  
  
    // Variables declaration - do not modify  
    // End of variables declaration  
  
}  


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}


(Both of the above codes are examples I copied, other users who used them said they worked fine)

Yet, it allows me to load an image using this code perfectly fine.
import java.awt.Image;

import javax.swing.ImageIcon;

public class Solid{
	protected int width, height;
	protected Image image;

	public Solid(String icon) {
        ImageIcon ii = new ImageIcon(this.getClass().getResource("images/" + icon));
        image = ii.getImage();
        width = ii.getIconWidth();
        height = ii.getIconHeight();
    }
}



Is there something I have to do in Eclipse before doing this? I haven't seen anything telling me to do so in any tutorials.

Also, I made sure the files were in the proper locations, I even tried doing exact locations using C://Users/...etc

Is This A Good Question/Topic? 0
  • +

Replies To: FileInputStream not detecting files in java - Eclipse

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: FileInputStream not detecting files in java - Eclipse

Posted 06 November 2011 - 04:45 PM

start by doing

System.out.println(this.getClass().getResource());

you will see from which directory you start with

you can also do

File f = new File("filepath");
if(f.exists())

to check if the file is where you think it should be


My bad, corrected further down

This post has been edited by pbl: 07 November 2011 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 Norix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 11

Re: FileInputStream not detecting files in java - Eclipse

Posted 06 November 2011 - 05:08 PM

So... Where do I put System.out.println(this.getClass().getResource());

I tried placing it in my code and I just get this:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: FileInputStream not detecting files in java - Eclipse

Posted 07 November 2011 - 05:40 AM

Create a folder under 'src' called 'music' and put the music files in that. Load them in the same way you're doing the image files

This post has been edited by g00se: 07 November 2011 - 05:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 Norix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 11

Re: FileInputStream not detecting files in java - Eclipse

Posted 07 November 2011 - 07:27 PM

View Postg00se, on 07 November 2011 - 05:40 AM, said:

Create a folder under 'src' called 'music' and put the music files in that. Load them in the same way you're doing the image files

I've already done that, and I just did it again, same problem.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: FileInputStream not detecting files in java - Eclipse

Posted 07 November 2011 - 08:48 PM

View PostNorix, on 06 November 2011 - 08:08 PM, said:

So... Where do I put System.out.println(this.getClass().getResource());

I tried placing it in my code and I just get this:
Posted Image

Sorry my bad

start in your constructor

    Sound() {
 	URL url = getClass().getResource("Sound.class");
        System.out.println(url);
        init();


That will give your where your code run from
Then you can follow g00se suggestion to build your subdirectory from there
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 04:11 AM

Quote

I've already done that, and I just did it again, same problem.


Actually, using my suggestion, this is how the resource should be addressed with music in src\music:

URL urlMusic = getClass().getResource("/music/" + musicFile);


and with your images in src\images:

URL urlImage = getClass().getResource("/images/" + imageFile);

Was This Post Helpful? 0
  • +
  • -

#8 Norix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 11

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 12:20 PM

View Postg00se, on 08 November 2011 - 04:11 AM, said:

Quote

I've already done that, and I just did it again, same problem.


Actually, using my suggestion, this is how the resource should be addressed with music in src\music:

URL urlMusic = getClass().getResource("/music/" + musicFile);


and with your images in src\images:

URL urlImage = getClass().getResource("/images/" + imageFile);


Thanks, but I've tried that as well, and it still hasn't worked :/


View Postpbl, on 07 November 2011 - 08:48 PM, said:

View PostNorix, on 06 November 2011 - 08:08 PM, said:

So... Where do I put System.out.println(this.getClass().getResource());

I tried placing it in my code and I just get this:
Posted Image

Sorry my bad

start in your constructor

    Sound() {
 	URL url = getClass().getResource("Sound.class");
        System.out.println(url);
        init();


That will give your where your code run from
Then you can follow g00se suggestion to build your subdirectory from there


Alright, I think I'm getting somewhere... But I got another error:
Posted Image

Here is my code after I put the lines in:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Sound extends javax.swing.JFrame {  
  
    /** Creates new form AudioLoopTest */  
    public Sound() {  
    	URL url = getClass().getResource("Sound.class");
    	System.out.println(url);
        initComponents();  
    }  

     public void playaudio() throws IOException{  
            System.out.println("Welcome in Audio Player");  
            InputStream in = new FileInputStream("music/one.mp3");  
            AudioStream as = new AudioStream(in);  
            
            AudioPlayer.player.start(as);
              
        }  
    
    @SuppressWarnings("unchecked")  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">  
    private void initComponents() {  
  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
        getContentPane().setLayout(layout);  
        layout.setHorizontalGroup(  
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
            .addGap(0, 400, Short.MAX_VALUE)  
        );  
        layout.setVerticalGroup(  
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
            .addGap(0, 300, Short.MAX_VALUE)  
        );  
  
        pack();  
    }// </editor-fold>  
  
    /** 
    * @param args the command line arguments 
    */  
    public static void main(String args[]) {  
        java.awt.EventQueue.invokeLater(new Runnable() {  
            public void run() {  
                new Sound().setVisible(true);  
                try {  
                    new Sound().playaudio();  
                } catch (IOException ex) {  
                    Logger.getLogger(Sound.class.getName()).log(Level.SEVERE, null, ex);  
                    System.out.println(ex.toString());  
                }  
            }  
        });  
    }  
  
    // Variables declaration - do not modify  
    // End of variables declaration  
  
}


(Putting the init(); in there game me this error along with the other error: Posted Image
So I just removed it.)
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,756
  • Joined: 19-March 11

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 12:32 PM

Easiest way to deal with this is to start with a JFileChooser and search out the exact file that you're looking for. Then you've got a File reference and you can use that to find out the exact path that you're looking for. Once you've found that, you can dike out the filechooser if you want.
file.getAbsolutePath() is a useful method here. If you want to be clever, you can write the path to a properties file, and bing, you're in business. Or you can just printeit out and hard-code it.

For your "URL cannot be resolved to to a type", you just need to import java.net.URL. Since you're using Eclipse, it should offer you a quick fix to do that for you.
Was This Post Helpful? 0
  • +
  • -

#10 Norix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 11

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 12:56 PM

View Postjon.kiparsky, on 08 November 2011 - 12:32 PM, said:

Easiest way to deal with this is to start with a JFileChooser and search out the exact file that you're looking for. Then you've got a File reference and you can use that to find out the exact path that you're looking for. Once you've found that, you can dike out the filechooser if you want.
file.getAbsolutePath() is a useful method here. If you want to be clever, you can write the path to a properties file, and bing, you're in business. Or you can just printeit out and hard-code it.

For your "URL cannot be resolved to to a type", you just need to import java.net.URL. Since you're using Eclipse, it should offer you a quick fix to do that for you.


I just threw together a JFileChooser, and made it load the absolute file path...
                        JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "mp3 & wav Images", "wav", "mp3");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(getParent());
            if(returnVal == JFileChooser.APPROVE_OPTION) {
               System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
            }
            System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
            System.out.println(chooser.getSelectedFile().getAbsolutePath());
            
               /////////////////////////////////////
            
            
            InputStream in = new FileInputStream(chooser.getSelectedFile().getAbsolutePath());  
            AudioStream as = new AudioStream(in);  
            AudioPlayer.player.start(as);


But it still gives me the same error. Even after I opened a .mp3, and a .wav.
It's not making any sense to me at this point.

Is there like, a setting or something that needs to be changed on eclipse?
I tried the same thing on another computer, thinking it was only mine, with the same results.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 02:34 PM

Quote

Thanks, but I've tried that as well, and it still hasn't worked



Using FileInputStream to load resources is less flexible than using classpath loading techniques. For one thing, using a relative path (as you are) means that you need to constrain the current directory

Using FileInputStream to load user-chosen files is perfectly ok, since it's a different use case, and as people have said, you can use JFileChooser and get a File from it, which should be foolproof as long as it's a file and readable
Was This Post Helpful? 0
  • +
  • -

#12 Norix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 11

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 02:44 PM

View Postg00se, on 08 November 2011 - 02:34 PM, said:

Quote

Thanks, but I've tried that as well, and it still hasn't worked



Using FileInputStream to load resources is less flexible than using classpath loading techniques. For one thing, using a relative path (as you are) means that you need to constrain the current directory

Using FileInputStream to load user-chosen files is perfectly ok, since it's a different use case, and as people have said, you can use JFileChooser and get a File from it, which should be foolproof as long as it's a file and readable


My music files are both files and readable. And it still has that error with JFileChooser
You're sure there's no settings I need to mess with anywhere?
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 03:11 PM

Can you please post your current code, preferably in runnable state
Was This Post Helpful? 0
  • +
  • -

#14 Norix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 11

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 03:20 PM

View Postg00se, on 08 November 2011 - 03:11 PM, said:

Can you please post your current code, preferably in runnable state

It's the code at the top of the post.

This is the code with the FileInputStream:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.URL;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Sound extends javax.swing.JFrame {  
  
    /** Creates new form AudioLoopTest */  
    public Sound() {  
    	URL url = getClass().getResource("Sound.class");
    	System.out.println(url);
    	//init();

        initComponents();  
    }  

     public void playaudio() throws IOException{  
            System.out.println("Welcome in Audio Player");  
            
            /////////////////////////////////////
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "mp3 & wav Images", "wav", "mp3");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(getParent());
            if(returnVal == JFileChooser.APPROVE_OPTION) {
               System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
            }
            System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
            System.out.println(chooser.getSelectedFile().getAbsolutePath());
            
               /////////////////////////////////////
            
            
            InputStream in = new FileInputStream(chooser.getSelectedFile().getAbsolutePath());  
            AudioStream as = new AudioStream(in);  
            AudioPlayer.player.start(as);
              
        }  
    
    @SuppressWarnings("unchecked")  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">  
    private void initComponents() {  
  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
        getContentPane().setLayout(layout);  
        layout.setHorizontalGroup(  
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
            .addGap(0, 400, Short.MAX_VALUE)  
        );  
        layout.setVerticalGroup(  
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
            .addGap(0, 300, Short.MAX_VALUE)  
        );  
  
        pack();  
    }// </editor-fold>  
  
    /** 
    * @param args the command line arguments 
    */  
    public static void main(String args[]) {  
        java.awt.EventQueue.invokeLater(new Runnable() {  
            public void run() {  
                new Sound().setVisible(true);  
                try {  
                    new Sound().playaudio();  
                } catch (IOException ex) {  
                    Logger.getLogger(Sound.class.getName()).log(Level.SEVERE, null, ex);  
                    System.out.println(ex.toString());  
                }  
            }  
        });  
    }  
  
    // Variables declaration - do not modify  
    // End of variables declaration  
  
}

Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,001
  • Joined: 20-September 08

Re: FileInputStream not detecting files in java - Eclipse

Posted 08 November 2011 - 03:28 PM

Your code works fine for me (on wav - afaik that player doesn't support mp3). Can you post the full error stack please?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2