Results 1 to 9 of 9
- 05-18-2009, 08:19 PM #1
[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.
- 05-18-2009, 11:27 PM #2
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
- 05-19-2009, 05:26 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-19-2009, 05:31 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Easily you can do the following.
Once you have to file list, simply you can open to read one-by-one.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]); } } }
- 05-19-2009, 05:32 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's what Java Doc says about the file security. Just a part of it. ;)First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument
- 05-19-2009, 06:08 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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.while((currFile = (File)ois.readObject()) != null){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!
- 05-19-2009, 10:27 AM #7
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 10:33 AM.
Ramya:cool:
- 05-20-2009, 04:51 PM #8
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)
...and like I said I forgot to comment out a method call. Carry on everyone, thanks for the help.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
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.
- 05-21-2009, 04:13 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's pleasure to help you lol. And thanks for the marking the thread as solved too.
Similar Threads
-
java.io.FileNotFoundException: ..\log\server.log (Access is denied.
By parimal in forum Advanced JavaReplies: 22Last Post: 04-09-2011, 04:59 AM -
Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02-10-2011, 09:55 AM -
'Permission denied' java script error
By java08 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-13-2009, 08:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks