aspose file tools*
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 Spring in Action this week in the Spring 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
Author

different results from same method call

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
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("email.properties");

recipients.txt and email.properties are in the same directory.

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

Why?

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 ]

JavaBeginnersFaq
"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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Have you tried editing (or renaming) the email.properties 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
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Yes, I have changed the contents of email.properties and the results changed to match my changes. I also tried renaming recipients.txt to recipients.properties 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
Wanderer
Sheriff

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
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
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: http://aspose.com/file-tools
 
subject: different results from same method call