Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Accessing local directory from a packaged class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

[Annoyingly pedantic mode]Who pronounces nerd ner-duh? Duh!??[/Annoyingly pedantic mode]
 
Nathan Popham
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might change that, but I was thinking of pronunciations by the "lesser intelligence".
 
Nothing up my sleeve ... and ... presto! A tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic