The Artima Developer Community
Sponsored Link

Java Answers Forum
Read from file

3 replies on 1 page. Most recent reply: May 2, 2003 12:56 PM by Lynn Hollerman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Read from file Posted: May 2, 2003 8:34 AM
Reply to this message Reply
Advertisement
I have a question concerning 2 ways of reading a file. Some background: I am currently maintaining a java-based application written by others, and while I strive to keep any modifications I make in agreement with their naming conventions and such, while I am testing my changes I don't always start out that way.

One of the things I had recently needed to do was change from using a hardcoded string to using a value read in from a file (and make sure that the value was read correctly). To do this, my initial thought was to use code like the following snippet:


java.io.FileReader fr = new java.io.FileReader("/properties/dbprop.properties");
java.io.Bufferedreader br = new java.io.BufferedReader(fr);
String str=null;
while ((str=br.readLine())!=null) {
System.out.println(str);
}

(in a try-catch, catching the FileNotFoundException and IOException)

"dbprop.properties" contained:

text

However, this wouldn't work.

In looking at the way the rest of the application did this same thing, I tried to duplicate that, which was

InputStream ins = this.getClass().getResourceAsStream("/properties/dbprop.properties");
Propertie s keyProps = new Properties();
key.Props.load(ins);
keystring = keyProps.getProperty("key");
System.out.println(keystring);


(also in a try-catch, catching the same exceptions)

"dbprop.properties" in this case was

key=text

As I said, this worked correctly, but I don't see what was wrong with the first way of doing it. Any ideas?

Thanks!

Lynn.


Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: Read from file Posted: May 2, 2003 10:01 AM
Reply to this message Reply
There's actually a pretty significant difference between the two approaches.

Note that the first approach (your own approach) is to open up a FileReader and wrap it into a BufferedReader, and then read through the file in the normal fashion. This is a fine approach for reading the contents of a file in the system's filesystem.

The second approach is very different. First of all, it does not use the system's filesystem at all -- note how it calls getResourceAsStream, passing it a path-like argument. But although this may look like a filesystem path, it doesn't have to be. Take a look at the JavaDoc for java.lang.Class.getResourceAsProperties and you'll see that it is a also way to get to a resource that is tucked away within a JAR or WAR file -- in which case the the resource isn't a file.

There is yet another difference in the second approach. Note the use of the java.util.Properties class. This is not the same as opening up a file and reading it either. While a Properties object can be built from a properties file, it can also be built from a properties resource that is not a file (such as one that is located in a JAR or WAR). This is one of the handier Java classes, because once you've created a Properties object in this fashion (handing it a properties file or properties resource with the load method), it does the work of parsing the file for you. You don't have to come up with a syntax parser to look for name-value pairs, then separate them, then store them. You can just call getProperties on the Properties object and pass in the String name of the property whose value you are searching for.

Usually when you simply want to store key/value pairs like this in a file (or a non-file resource in a JAR/WAR), using a properties file is the easiest way to do it. It's fully supported by Java, and many other languages provide a means to read a properties file. And if you had to directly parse the file, it's not very complicated (though there are more ways to write it than just "name=value"). I recommend you read the java.util.Properties JavaDoc, it'll explain everything.

Note that there is a new class called java.util.Preferences which works similar to Properties, but isn't quite as easy to use (but is somewhat more flexible).

Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: Read from file Posted: May 2, 2003 10:06 AM
Reply to this message Reply
Um, it just occurred to me that I never really answered your question. I apologize.

The problem may be that there is no terminating newline or carriage return/linefeed character at the end of your file. The BufferedReader.readLine method will only read a line if it is truly a "line", and BufferedReader only considers strings of text that end with a newline to be true "lines".

Try hitting return at the end of your dbprop.properties line of text and see if that changes your results.

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: Read from file Posted: May 2, 2003 12:56 PM
Reply to this message Reply
Actually, BOTH your answers were EXACTLY what I needed to know!

THANK YOU!!!

Lynn.

Flat View: This topic has 3 replies on 1 page
Topic: Threads treading on one another's feet... Previous Topic   Next Topic Topic: help with a program in java that converts IP addresses into binary

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use