Solved

About properties file

Posted on 2003-11-24
8
1,178 Views
Last Modified: 2013-11-23
I am working on java applet and all .class files, images and text files will be loaded from the
applet archive attribute jar file named your_login.jar. My
program will first load a configuration file named library.properties
from my classpath /config/library.properties. From there my
program will load the employees.txt and books.txt file also in the
config subdirectory - see below jar file content. I am not supposed
 hard-code
employee/book values but instead read them in at runtime using the
following files: employees.txt and books.txt that I should include
in my jar file config directory. I know that I could
use this.getClass().getResource("/config/library.properties");
to get a URL to the file located at config/library.properties
in my jar file. I think I need to use getResourceAsStream to get an InputStream
to that resource.


your_login.jar (library jar file loaded by applet) contains the following:

jar xvf your_login.jar will create relative to the current directory:
your_login/library/Book.class
your_login/library/Employee.class
...
config/library.properties
config/employees.txt
config/books.txt
images/CheckOut.gif
images/CheckIn.gif

My question is how my library.properties file should be?

thanks,


0
Comment
Question by:dkim18
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Assisted Solution

by:DidierD
DidierD earned 20 total points
ID: 9816284
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9816288
If your question is how to format your properties file, then the following is the format:

    [key]=[value]

Here is an example;

   myKey=myValue

Don't forget that the same characters that you would escape in s String constant in Java, also needs to be escaped in your properties file.
0
 
LVL 92

Expert Comment

by:objects
ID: 9816289
Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/config/library.properties"));

And your properties file should be in the format:

name1=valu1
name2=value2
0
 

Author Comment

by:dkim18
ID: 9827188
I did the following, but I got FileNotFoundExcetion error.

In my readEmployeeFile() fuction:

Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/config/library.properties"));
String bookResource = props.getProperty("books");

inFile = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(bookResource)));

and inside of library.properties:

books="classes\config\books.txt"
employees="classes\config\employees.txt"

I also created library.properties solaris system and ftp into my home pc because I was told library.properties shouldn’t .txt format. I tried changing library.properties executable file too, but wasn’t successful.
In my config dir, books.txt and employees.txt are located, but I shouldn’t hardcode like this:
inFile = new BufferedReader(new
InputStreamReader(this.getClass().getResourceAsStream("/config/books.txt")))

but instead read them from the library.properties

What am I doing wrong?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 80 total points
ID: 9827423
Try using:

   public URL Class.getResource(String name)

and see what the URL is pointing to by calling either toString() or toExternalForm().

According to the Javadocs on getResource:

"Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the * defining class loader of the class.

This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource. "
0
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 9827674
WHere exactly are you getting the error, and where are your properties and txt files located?

> books="classes\config\books.txt"
> employees="classes\config\employees.txt"

should perhaps be:

books=/config/books.txt
employees=/config/employees.txt


0
 

Author Comment

by:dkim18
ID: 9828127
books=/config/books.txt
employees=/config/employees.txt

this solved the problem.
Thank you so much...
0
 
LVL 92

Expert Comment

by:objects
ID: 9828148
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 52
Checkbox and ListView in Android Layout 4 56
Delphi: Connect to running MS Outlook 4 47
java jdbc batch example 8 19
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now