Results 1 to 9 of 9
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default [SOLVED] Access Denied error using FileInputStream

    I have some music stored on an external harddrive that I want to play around with using a java program(right now just displaying the files) and I keep getting a FileNotFoundException saying access is denied to file(e:\Music). However file returns true when I call exists() and canRead(). Anybody know how to fix this?

    Java Code:
    public class SortMusic {
    	static File file = new File("E:\\Music");
    	static FileInputStream fis = null;
    	static ObjectInputStream ois = null;
    	
    	public static void main(String args[]){
    		init();
    		listArtists();
    	}
    	public static void init(){
    		//Directory = E:\Music
    		System.out.println(file.canRead());
    		System.out.println(file.exists());
    		try {
    			fis = new FileInputStream(file);
    		} catch (FileNotFoundException e) {
    			System.out.println("File not found");
    			e.printStackTrace();
    		}
    		try {
    			ois = new ObjectInputStream(fis);
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (NullPointerException e){
    			System.out.println("input stream is null");
    		}
    	}
    	public static void listArtists(){
    		File currFile = null;
    		ArrayList<File> artists = new ArrayList<File>();
    		try {
    			while((currFile = (File)ois.readObject()) != null){
    				artists.add(currFile);
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		for (int i = 1; i <= artists.size(); i++) {
    			System.out.println(artists.get(i));
    		}		
    	}
    }
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It's not a directory is it?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    This can be due to two reason. Either you are locating an ordinary file, not a directory or the directory attributes should be read only.

    Seems to me, the first reason cannot be valid according to your application. So check the second first of all.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Easily you can do the following.

    Java Code:
    	public static void main(String args[]){
            File file = new File("H:\\Fun\\Songs\\Micheal Loreance To Rock");
    		File[] myMusicFiles = file.listFiles();
    
            for(int index = 0; index < myMusicFiles.length; index++) {
                if(myMusicFiles[index].isFile()) {
                    System.out.println("File: " + myMusicFiles[index]);
                }
            }
    	}
    Once you have to file list, simply you can open to read one-by-one.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument
    That's what Java Doc says about the file security. Just a part of it. ;)

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    while((currFile = (File)ois.readObject()) != null){
    Looks to me like you are trying to read a file as a file... not likely to work, since you would have had to write a file into a file in the first place, which would just be... odd. especially since File is just an abstract pathname. You probably want to use File.listFiles() to get the array of files, then print File.getName(). Mind you, files are not my area of expertise, but I'm pretty sure E:\Music would be a directory.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Hi,
    For FileInputStream/FileoutputStream u can't pass the directory.Please go thru the Java doc for constructor details.

    FileInputStream
    public FileInputStream(File file)
    throws FileNotFoundExceptionCreates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. A new FileDescriptor object is created to represent this file connection.
    First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument.

    If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown.


    FileOutputStream
    public FileOutputStream(File file)
    throws FileNotFoundExceptionCreates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is created to represent this file connection.
    First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument.

    If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.


    Parameters:
    file - the file to be opened for writing.
    Throws:
    FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
    SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
    See Also:
    File.getPath(), SecurityException, SecurityManager.checkWrite(java.lang.String)
    Last edited by RamyaSivakanth; 05-19-2009 at 11:33 AM.
    Ramya:cool:

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    I can't believe it didn't occur to me that my music folder wasn't in fact a file(girlfriends in the emergency room so I've been distracted).

    @Eranga: Thank you that snippet was exactly what I needed
    @Singing Boyo: It was a looooong day, as you can tell my brain wasn't fully functioning lol.

    I'm curious why Danzig throws a NPE yet the code continues to run(maybe I should look at my code before posting but hey it's still early)
    Java Code:
    Directory: Flogging Molly
    Directory: Drowning Pool
    Directory: Adema
    Directory: DanzigException in thread "main" java.lang.NullPointerException
    	at pack.SortMusic.listArtists(SortMusic.java:56)
    	at pack.SortMusic.main(SortMusic.java:18)
    
    Directory: Pantera
    Directory: Stone Temple Pilots
    Directory: 30 Seconds To Mars
    ...and like I said I forgot to comment out a method call. Carry on everyone, thanks for the help.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 22
    Last Post: 04-09-2011, 05:59 AM
  2. Replies: 6
    Last Post: 02-10-2011, 10:55 AM
  3. 'Permission denied' java script error
    By java08 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-13-2009, 09:19 AM

Posting Permissions

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