Problem with storing properties

M

Mike

Hi,
I try to store and retrieve properties from an ini file and tried the
following code:

import java.io.*;
import java.util.*;

public class testSetProperty {
public static void main(String[] args) {
File f = new File("test.ini");
try{FileOutputStream outfile = new FileOutputStream(f);
} catch (IOException e) {}
Properties props = new Properties();
props.setProperty("key1","value1");
try {props.store(outfile,"Nothing special");
} catch (Exception ex) {}
}
}

I get however an error:

testSetProperty.java:10: cannot resolve symbol
symbol : variable outfile
location: class Helloworld
try {props.store(outfile,"Nothing special");} catch (Exception ex)
{}
^

What is wrong in my code?
I am working on a linux box with (java -version):
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build
Blackdown-1.3.1-02b-FCS)
Java HotSpot(TM) Client VM (build Blackdown-1.3.1_02b-FCS, mixed mode)

Thanks.

Mike
 
M

Mike Bosschaert

Erratum:
"location: class Helloworld"
of course should read
"location: class testSetProperty"

(I mixed up some messages, excuse, Mike)
 
O

Oscar kind

Mike said:
Hi,
I try to store and retrieve properties from an ini file and tried the
following code:

import java.io.*;
import java.util.*;

public class testSetProperty {
public static void main(String[] args) {
File f = new File("test.ini");
try{FileOutputStream outfile = new FileOutputStream(f);
} catch (IOException e) {}

Note thet the variable "outfile" if defined in the try block; it doesn't
exist outside the try block.
Properties props = new Properties();
props.setProperty("key1","value1");
try {props.store(outfile,"Nothing special");
} catch (Exception ex) {}
}
}

I get however an error:

testSetProperty.java:10: cannot resolve symbol
symbol : variable outfile
location: class Helloworld
try {props.store(outfile,"Nothing special");} catch (Exception ex)
{}
^

What is wrong in my code?

You defined "outfile" in the try block. Declare it just before the try
block and assign it inside the try block (as you do now).

Another option (the one I prefer), if to set the properties before the try
block, and do all the output (declaring "outfile", calling "store(...)",
....) inside the try block.


Oscar
 
Y

Yu SONG

Mike said:
Hi,
I try to store and retrieve properties from an ini file and tried the
following code:

import java.io.*;
import java.util.*;

public class testSetProperty {
public static void main(String[] args) {
File f = new File("test.ini");
try{FileOutputStream outfile = new FileOutputStream(f);
} catch (IOException e) {}
Properties props = new Properties();
props.setProperty("key1","value1");
try {props.store(outfile,"Nothing special");
} catch (Exception ex) {}
}
}

I get however an error:

testSetProperty.java:10: cannot resolve symbol
symbol : variable outfile
location: class Helloworld
try {props.store(outfile,"Nothing special");} catch (Exception ex)
{}
^

What is wrong in my code?
I am working on a linux box with (java -version):
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build
Blackdown-1.3.1-02b-FCS)
Java HotSpot(TM) Client VM (build Blackdown-1.3.1_02b-FCS, mixed mode)

Thanks.

Mike

Not in the same block...


put

FileOutputStream outfile = null;

before the 1st try block...

Or put

FileOutputStream outfile = new FileOutputStream(f);
into the 2nd try block and remove the 1st one.(much better)
 
T

Tony Morris

You defined "outfile" in the try block. Declare it just before the try
block and assign it inside the try block (as you do now).

This will result in a compile-time error, since outfile can't be used if it
hasn't been assigned (which is not guaranteed).

There are a couple of points to note:
- NEVER EVER ignore a checked exception like you have done.
- NEVER EVER declare to catch java.lang.Exception unless you rethrow it
(which you don't).

These two things are sins, with dire consequences.
Now let's fix your code:

File f = new File("test.ini");

FileOutputStream outfile = null;

try{
outfile = new FileOutputStream(f);

Properties props = new Properties();
props.setProperty("key1","value1");
props.store(outfile,"Nothing special");
}
catch (IOException e) {
ioe.printStackTrace();
}
finally {
if(outfile != null) {
try {
outfile.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}

You might benefit from reading the exceptions tutorial on Sun.
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top