Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading a file from a parent directory??

 
Brad Donaldson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
Brad Donaldson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it throw a SecurityException ?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic