Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem with storing properties

Reply
Thread Tools

Problem with storing properties

 
 
Mike
Guest
Posts: n/a
 
      05-31-2004
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
 
Reply With Quote
 
 
 
 
Mike Bosschaert
Guest
Posts: n/a
 
      05-31-2004
Erratum:
"location: class Helloworld"
of course should read
"location: class testSetProperty"

(I mixed up some messages, excuse, 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


 
Reply With Quote
 
 
 
 
Oscar kind
Guest
Posts: n/a
 
      05-31-2004
Mike <(E-Mail Removed)> wrote:
> 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

--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 
Reply With Quote
 
Yu SONG
Guest
Posts: n/a
 
      06-01-2004
Mike wrote:
> 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)

--
Song

More info.:
http://www.dcs.warwick.ac.uk/~esubbn/

 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      06-07-2004
> 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/tutor...ons/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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing style properties aside from object vunet Javascript 2 09-10-2008 06:02 PM
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
Problem storing properties file in vista brownk13@gmail.com Java 4 05-20-2007 09:00 AM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
Making Custom Control Properties Visible in Visual Studio's Properties Palette Nathan Sokalski ASP .Net 0 10-17-2005 02:05 AM



Advertisments