× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



No problem. I have been down this path before and personally just found the
getResourceAsStream() much easier to use. The other option you can look into
are loading it as a ResourceBundle.

The FileInputStream will read a file in the classpath, but not in a any
compressed file (JAR/ZIP). It expects an actual File object or it will
create it's own File object if you pass the constructor a String. Imagine
FileInputStream as notepad. If you were to open a zip file with notepad it's
not human readable.

--
James R. Perkins


On Tue, May 5, 2009 at 11:41, Lim Hock-Chai
<Lim.Hock-Chai@xxxxxxxxxxxxxxx>wrote:

That work. Thanks James.

Is it safe to say that FileInputStream does not understand classpath and
it does not know how to read a file that is inside of a jar either?

"James Perkins" <jrperkinsjr@xxxxxxxxx> wrote in message
news:<mailman.5245.1241543851.23468.web400@xxxxxxxxxxxx>...
Actually, now that you say that it's not. Try this.
public class TestClass2 {
public static void main(String[] args) {
InputStream myFileIS = null;
try {
myFileIS =
SomeClassInYourZipFile.class.getClassLoader().getResourceAsStream(
"boss.properties");
Properties prop = new Properties();
prop.load(myFileIS);
System.out.println("loaded");
for (Map.Entry<Object, Object> type : prop.entrySet()) {
System.out.println(type.getKey() + "=" +
type.getValue());
}
} catch (Exception ex) {
System.out.println(ex);
} finally {
try {
myFileIS.close();
} catch (Exception e) {
}
}
}
}

--
James R. Perkins


On Tue, May 5, 2009 at 09:53, Lim Hock-Chai
<Lim.Hock-Chai@xxxxxxxxxxxxxxx>wrote:

I tested it on my local and here is what I see:
The FileUtil.class.getClassLoader().getResource( "boss.properties")
returns a File object with the path as
"file:\C:\jar\Boss.zip!\boss.properties"
When I try to create a FileInputStream with this File object, it
failed
with "java.io.FileNotFoundException:
file:\C:\jar\Boss.zip!\boss.properties (The filename, directory
name, or
volume label syntax is incorrect)".

It seems like FileInputStream is not capable of understanding
classpath
variable and/or not capable of reading a file that is in a zip file.

Below is my test program:
public class Test2 {

public static void main(String[] args) {

File myFile = FileUtil.findFile("boss.properties");
try {
FileInputStream myFileIS = new
FileInputStream(myFile);
Properties prop = new Properties();
prop.load(myFileIS);
System.out.println("loaded");

} catch (Exception ex) {
System.out.println(ex);
}
System.out.println(myFile);
}
}

"James Perkins" <jrperkinsjr@xxxxxxxxx> wrote in message
news:<mailman.5204.1241539916.23468.web400@xxxxxxxxxxxx>...
You actually shouldn't have to change your code. The using of
InputStream
was just a suggestion. It's always the best to use the highest
level
object
you can. It just helps you change from a FileInputStream to say a
StringBufferInputStream without changing other code.

If you can debug it, I would check to see what the name of the
file is
that
is returned and make sure it's in the JAR or at least your
classpath.
The
code looks fine, but my guess would be that it's not found in the
classpath.
--
James R. Perkins


On Tue, May 5, 2009 at 08:42, Lim Hock-Chai
<Lim.Hock-Chai@xxxxxxxxxxxxxxx>wrote:

Thanks James, I was hoping that we do not have to change code
for
this.
My project is to move our J2EE application from jBOSS to WAS6.1.

Below are relevent code that does the setFile:

public static File findFile( String fileName ) {
File file = null;
URL url = FileUtil.class.getClassLoader().getResource(
fileName
);
if( url != null ) {
String decodedUrl = null;
try {
decodedUrl = URLDecoder.decode( url.getPath(),
"ASCII"
);
} catch( UnsupportedEncodingException uee ) {
uee.printStackTrace();
}
file = new File( decodedUrl );
}
return file;
}


I'll look to see what the different between FileInputStream in
InputStream.

Thanks
--
This is the Web Enabling the AS400 / iSeries (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/web400.


--
This is the Web Enabling the AS400 / iSeries (WEB400) mailing list
To post a message email: WEB400@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/web400
or email: WEB400-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/web400.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.