This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes different results from same method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "different results from same method call" Watch "different results from same method call" New topic

different results from same method call

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
I have a class with 2 methods, one to read a properties file and the other to read a text file. The methods are almost identical in this one point ...

InputStream input = EMailer.class.getResourceAsStream("recipients.txt");
InputStream input = EMailer.class.getResourceAsStream("");

recipients.txt and are in the same directory.

Getting works fine.
Getting recipients.txt returns a null InputStream.


I've checked the spelling (for typos) at least 10 times. Even used copy/paste.

I can hard code the path

File base = new File("/java");
File file = new File(base, "recipients.txt");
InputStream input = new FileInputStream(file);

And that works just fine, but it's not portable enough for my needs.

How can I get the text file without hard coding it?
[ November 03, 2007: Message edited by: Marilyn de Queiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Have you tried editing (or renaming) the that you think is being loaded, and ensuring that the behavior changes? Maybe it's a different one that's being loaded.

[Jess in Action][AskingGoodQuestions]
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Yes, I have changed the contents of and the results changed to match my changes. I also tried renaming recipients.txt to and interestingly (I thought), magically, the InputStream is no longer null and the app works. However, I really don't want to name a text file with a bunch of email addresses with a .properties extension since it isn't really properties.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Very strange.

I don't suppose you're on Windows and have Explorer set to hide file extensions for known types? Then "text.txt" would appear as "text", and "test.txt.jpg" would appear as "test.txt". So maybe the thing that looks like recipients.txt is really something else.

Or, here's an alternate approact that might she some light if it yields unexpected results:

In principle there's no reason this will give you any different answer from what you've tried so far. But then, in principle you shouldn't be having this problem to begin with, right? So what the heck...

"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
That was an interesting experiment. Apparently a case of my IDE trying to "help" me by copying .properties files (resources) to the build directory where the classes are, but not copying other files there. Thank you.
[ November 05, 2007: Message edited by: Marilyn de Queiroz ]
I agree. Here's the link:
subject: different results from same method call
It's not a secret anymore!