Problem with storing properties

Discussion in 'Java' started by Mike, May 31, 2004.

  1. Mike

    Mike Guest

    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
     
    Mike, May 31, 2004
    #1
    1. Advertising

  2. 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
     
    Mike Bosschaert, May 31, 2004
    #2
    1. Advertising

  3. Mike

    Oscar kind Guest

    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) {}


    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
     
    Oscar kind, Jun 1, 2004
    #3
  4. Mike

    Yu SONG Guest

    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/
     
    Yu SONG, Jun 1, 2004
    #4
  5. Mike

    Tony Morris Guest

    > 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
     
    Tony Morris, Jun 7, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    0
    Views:
    921
    Nathan Sokalski
    Oct 17, 2005
  2. toton
    Replies:
    11
    Views:
    714
    toton
    Oct 13, 2006
  3. Replies:
    4
    Views:
    353
    Philipp Leitner
    May 20, 2007
  4. Jonathan Wood
    Replies:
    1
    Views:
    511
    Jonathan Wood
    Jun 2, 2008
  5. vunet
    Replies:
    2
    Views:
    90
    vunet
    Sep 10, 2008
Loading...

Share This Page