File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes reading a file from a parent directory?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "reading a file from a parent directory??" Watch "reading a file from a parent directory??" New topic
Author

reading a file from a parent directory??

Brad Donaldson
Greenhorn

Joined: Jul 12, 2001
Posts: 4
Hi,
I have used a jar file to house the class files for an applet,
and in the applet i want to read a txt file.
I am thinking that (as java seems to treat the jar file as a subdirectory) it would be the same as calling a file from a parent directory.
I've tried:
input = new BufferedReader(new FileReader("..\\data.txt");
but this doesn't seem to do the trick. can anyone help
Thanx, Brad.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If I understand what you're trying to do, the location of your class files inside a jar file is irrelevant. The important question is - what is the working directory for your java application? That is, what is your current directory when you type "java MyApplication" or whatever? And where is the target file data.txt relative to that directory?
If you can't work out the correct relative path, just use the absolute path, e.g.
input = new BufferedReader(new FileReader("C:\\Java\\Programs\\data.txt");


"I'm not back." - Bill Harding, Twister
Brad Donaldson
Greenhorn

Joined: Jul 12, 2001
Posts: 4
Jim.
the directory structure is as follows:
c:\java\applets\applethtml.html
c:\java\applets\data.txt
and the class files are inside the jar file:
c:\java\applets\appletjar.jar
i have tried putting in just ....leReader("data.txt");
and that didnt work either.
I would put in the absolute path but it is going to be put on heaps of machines and in different locations (and i dont really want to have to recompile it with different paths for all the machines).
Omar IRAQI
Ranch Hand

Joined: Jul 06, 2001
Posts: 54
Does it throw a SecurityException ?


Omar IRAQI Houssaini
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Do a

to find out what the current working directory really is. Also, do try to run the applet using the full path name. I understand this isn't desirealbe as your final solution, but it may provide useful information about what's going on. If you can do it using the full path, then it's just a problem of determining the correct relative path. Otherwise, something else is wrong. Good luck.
Brad Donaldson
Greenhorn

Joined: Jul 12, 2001
Posts: 4
Thanx Jim,
I tried: System.out.println(System.getProperty("user.dir"));
(as you said) and it worked nicely.
... well that was untill i came to depolying it :-(
i have been using JBuilder 5 to make it and it works fine when i run it from within JBuilder, but when i try to run it on its own (ie. in an internet browser) it doesnt work properly... any part of the program to do with the file doesnt do anything.
eg. there is a button that is ment to execute a method that looks through the file sort of like a search engine, but when you press the button nothing happens.
Is this something to do with JBuilder assuming stuff that the browser doesnt?
(Omar, it doesnt throw a security exception, there is no security in the program at all)
Brad Donaldson
Greenhorn

Joined: Jul 12, 2001
Posts: 4
Correction:
On compiling and running the applet through msdos, i have seen that indeed it does throw a security exception as follows...
Exception occured during event dispatching:
java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read)
I didnt really want to use any security because i dont know much about it, but is there a simple fix?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I don't know much about security and applet issues either. I think it depends on how much control you have over the client environment - will all your users be using a particular browser? Can you expect them to configure it a certain way, or are you stuck with whatever security settings they provied? I see that on IE 5.5, you can configure Java security options using Tools -> Internet Options -> Security -> Custom Level -> Microsoft VM -> Custom Level. Well, the's for the exact specifics. At a higher lever, try just going to Security and listing your site as a "Trusted" site rather than default internet. If that's not enough, customize the access which "trusted" sites are allowed. Otherwise, your best bet is probably the Applets forum, or Susn't tutorial. Good luck...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading a file from a parent directory??