Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading/Writing to a file 1

Status
Not open for further replies.

vza

Programmer
Aug 1, 2003
179
US
I want to store connection properties (URL/Username/Password) for a DB to a file and also have the ability to access these properties as needed.

What would be the best way to do this?

-vza
 
I would suggest to use the Properties class.

U can specify your own keys with their values.

These properties/file are always accessible using the class.

Kind regards,

Madere
 
Alternatively (and this is only a slight variant), you can use the "getProperty" method of the System class. I use this in conjunction with the "-D" option in the java invocation. That is, in my code, I have:
FileIn = System.getProperty("user.home")+File.separator+"filename.ext";
In my invocation, I have:
java -cp <blah blah> -Duser.home="c:\abc\def" <classname>


_________________
Bob Rashkin
rrashkin@csc.com
 
Madere,
I took your advise and tried to utilize the Properties class:


Properties wwdConnect = new Properties();
wwdConnect.setProperty("URL", "DB_URL");
wwdConnect.setProperty("Username", "DB_USER");
wwdConnect.setProperty("Password", "DB_PASS");

This is my Save Properties method (shown later):
saveProperties();

I then set the properties to my JTextField Textboxes:

this.setURL(wwdConnect.getProperty("URL"));
this.setUsername(wwdConnect.getProperty("Username"));
this.setPassword(wwdConnect.getProperty("Password"));

Here is my save Properties method:
public void saveProperties()
{
try
{
FileOutputStream output = new FileOutputStream("Connection.txt");
wwdConnect.store(output, "Connection" );
output.close();
}
catch (IOException ioException)
{
ioException.printStackTrace();
}//end catch
}//End saveProperties method

I recieve a NPE with the store method...any suggestions?
Any responses would be greatly appreciated.

-vza
 
vza,

I have copied your code and I can't find any problem.

Here is my part:
***********************************
Properties wwdConnect = new Properties();

public void FillProps()
{
wwdConnect.setProperty("URL", "DB_URL");
wwdConnect.setProperty("Username", "DB_USER");
wwdConnect.setProperty("Password", "DB_PASS");
}

public void SaveProps()
{
try
{
FileOutputStream output = new FileOutputStream("Connection.txt");
wwdConnect.store(output, "Connection" );
output.close();
}
catch (IOException ioException)
{
ioException.printStackTrace();
}//end catch
}//End saveProperties method

public void DoProps()
{
FillProps();
SaveProps();
}

public static void main(..)
{
..
test.DoProps();
..
}
***********************************

Have you already checked if the "Connection.txt" is created?
Or you might have 2 instances called wwdConnect?

Try do comment out your line "wwdConnect.store(output, "Connection" );" and check what happens.

Good luck.

Madere.
 
If the properties file is located in the same package as MyClass, you could read it in as a Properties instance with something like this:-

Code:
public class MyClass {
  private static Properties props;

  static {
    InputStream isResource = MyClass.getResourceAsStream("connection.properties");
    props = new Properties();
    try {
      props.load(isResource);
    } catch ( IOException ex ){
      //Resource unavailable
      //Consider populating props with defaults
    }
  }

}

The properties are then statically available to this classes instances.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Madere,
when I comment out the following line:

wwdConnect.store(output, "Connection");
my saveProperties method is skipped entirely...
(When debugging nothing in the method is executed unless the previous line in uncommented.

I did notice something while I was compiling:

DBConnectWindow.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
My class is an extension of JFrame.

Is this something which would cause this exception? How would I go about fixing this problem?

Thanks for all your help with this issue.

-vza
 
I think I figured it out....
For some reason when i run the saveProperties method my Properties table is now null, even though the Properties table is populated before the saveProperties method is called...is there a reason why the values are not carried over??


Properties wwdConnect = new Properties();
wwdConnect.setProperty("URL", "DB_URL");
wwdConnect.setProperty("Username", "DB_USER");
wwdConnect.setProperty("Password", "DB_PASS");

this.setURL(wwdConnect.getProperty("URL"));
this.setUsername(wwdConnect.getProperty("Username"));
this.setPassword(wwdConnect.getProperty("Password"));

saveProperties();

...

public void saveProperties()
{
try
{
FileOutputStream output = new FileOutputStream("Connection.txt");
wwdConnect.store(output, "Connection" );
output.close();
}
catch (IOException ioException)
{
ioException.printStackTrace();
}//end catch
}//End saveProperties method

Upon debuggin within the method wwdConnect value is null...even though values are applied to the property table earlier...

-vza


 
vza,

I see you have made a basic Java programming error.
I think you have 2 variables called wwdConnect (as I mentioned before).

You must have a class member called wwdConnect which is not initialized. Before you call the saveProperties() method you create a new wwdConnect variable again. But in the saveProperties() methid you work with the empty class member wwdConnect. Because you did not fill/instantiate this one, you will get a NullPointerException.

Check your code again please.

Kind regards,

Madere.
 
Madere,

You were right.
I created wwdConnect at the beginning of my class:

private Properties wwdConnect;

Then when I wanted to define it within my class, I used the following:

Properties wwdConnect = new Properties();

Which ultimately created a new instance to my Properties variable and assigned it null.

As I am sure you can tell I am kinda new to all this Java stuff...

Thanks for all your help.

-vza
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top