| Author |
Accessing local directory from a packaged class
|
Nathan Popham
Greenhorn
Joined: Sep 23, 2009
Posts: 9
|
|
Hi. I haven't done much in Java, but I'm taking another class in it, so I'll probably be asking questions here a lot. Most of what I've learned has been from Actionscript 3, and my focus there (games ) is different from what I do with Java (assignments ).
Sorry if this question has already been asked, I did search for it but couldn't find it. (Believe me, I would much prefer to be silent.)
Anyway, I was having trouble accessing a textfile from my program, which I had in a package called assignment1 so I could reference it. The text file was in the same directory as the program, and I was trying to access it by name (just a reader with "customer.txt").
After trial and error, I found that it couldn't find customer.txt because the program was in a package. I've found out I don't even need a package, so I've taken that out, and it works. But I want to find out how to access a local file from a program in a package, and what exactly putting a program in a package does that would make it unable to find a file in its own directory.
Thanks!
EDIT: I'm not sure I made sense. So I'll make sure.
I put my program, BankDriver, in a package (and folder directory inside my path) called assignment1. Inside that folder was a textfile called customer.txt. BankDriver tried to access customer.txt (using a reader passed "customer.txt"), but couldn't find it . I took out the line "package assignment1;" (after a long long time) and it worked, so, I guess a program inside a package can't find it's local directory. Basically, how could the program find the local directory and why didn't it work?
|
 |
Alberto Pareja-Lecaros
Greenhorn
Joined: Sep 23, 2009
Posts: 2
|
|
Your .txt file needs to be in the top level directory and not in the source folder of your java classes. So if your directory structure were like this:
/programs/program1/ <- this contains your Java .class files and maybe your .java also
/programs <-that's where your .txt file needs to go.
Try that and see if that works.
|
http://www.java-made-easy.com for tutorials and help with Java
|
 |
Zandis Murāns
Ranch Hand
Joined: Aug 18, 2009
Posts: 174
|
|
You can always check on what directory you are sitting on at the moment in very trivial way - by printing out current directory's path:
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Welcome to JavaRanch
Package names should be used for all but small or temporary applications; the things you are doing as beginners' assignments would count as "small". Once you have more than about a dozen .java files, you should use a package structure, but compiling files in packages is more complicated. So you are better off leaving the package names out for the time being.
|
 |
Nathan Popham
Greenhorn
Joined: Sep 23, 2009
Posts: 9
|
|
@Alberto -- That did work. Thanks!
@Zandis -- Thanks for that too. I've wanted to know how to do that a couple times now.
@Campbell -- Yeah, you're right. I took it out.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
You're welcome
[Annoyingly pedantic mode]Who pronounces nerd ner-duh? Duh!??[/Annoyingly pedantic mode]
|
 |
Nathan Popham
Greenhorn
Joined: Sep 23, 2009
Posts: 9
|
|
I might change that, but I was thinking of pronunciations by the "lesser intelligence".
|
 |
 |
|
|
subject: Accessing local directory from a packaged class
|
|
|