how write value to file?

Discussion in 'Java' started by Geoff Cox, Jul 21, 2005.

  1. Geoff Cox

    Geoff Cox Guest

    Hello,

    I'm not clear how to write a vlaue to a file on the hard disk ... I
    have the code below and wish to write the getValue() number to disk.

    I am guessing that I need to call (with this value) a method to do
    this but my following effort is wrong. Please see also the rest of the
    code after my signature

    writeToFile(scale.getValue());

    }

    public static String writeToFile (int num)
    {
    try
    {
    FileWriter file = new FileWriter("data.txt");
    BufferedWriter buffer = new BufferedWriter(file);
    buffer.write(num);
    buffer.close();
    }
    catch(IOException e) { System.out.println(e); }
    }

    Help! please

    Geoff

    --------------------

    public void stateChanged(ChangeEvent event)
    {
    JSlider src = (JSlider) event.getSource();
    if(!src.getValueIsAdjusting())
    position.setText("Position is
    "+scale.getValue());

    writeToFile(scale.getValue());

    }

    public static String writeToFile (int num)
    {
    try
    {
    FileWriter file = new FileWriter("data.txt");
    BufferedWriter buffer = new BufferedWriter(file);
    buffer.write(num);
    buffer.close();
    }
    catch(IOException e) { System.out.println(e); }
    }
     
    Geoff Cox, Jul 21, 2005
    #1
    1. Advertising

  2. Thomas Weidenfeller, Jul 21, 2005
    #2
    1. Advertising

  3. Geoff Cox

    Sharp Tool Guest

    I use PrintWriter class:

    PrintWriter pw = new PrintWriter(new FileWriter("data.txt"));
    pw.println(num);
    pw.close();

    Cheers
    Sharp


    "Geoff Cox" <> wrote in message
    news:...
    > Hello,
    >
    > I'm not clear how to write a vlaue to a file on the hard disk ... I
    > have the code below and wish to write the getValue() number to disk.
    >
    > I am guessing that I need to call (with this value) a method to do
    > this but my following effort is wrong. Please see also the rest of the
    > code after my signature
    >
    > writeToFile(scale.getValue());
    >
    > }
    >
    > public static String writeToFile (int num)
    > {
    > try
    > {
    > FileWriter file = new FileWriter("data.txt");
    > BufferedWriter buffer = new BufferedWriter(file);
    > buffer.write(num);
    > buffer.close();
    > }
    > catch(IOException e) { System.out.println(e); }
    > }
    >
    > Help! please
    >
    > Geoff
    >
    > --------------------
    >
    > public void stateChanged(ChangeEvent event)
    > {
    > JSlider src = (JSlider) event.getSource();
    > if(!src.getValueIsAdjusting())
    > position.setText("Position is
    > "+scale.getValue());
    >
    > writeToFile(scale.getValue());
    >
    > }
    >
    > public static String writeToFile (int num)
    > {
    > try
    > {
    > FileWriter file = new FileWriter("data.txt");
    > BufferedWriter buffer = new BufferedWriter(file);
    > buffer.write(num);
    > buffer.close();
    > }
    > catch(IOException e) { System.out.println(e); }
    > }
     
    Sharp Tool, Jul 21, 2005
    #3
  4. Geoff Cox

    Geoff Cox Guest

    On Thu, 21 Jul 2005 08:14:28 GMT, "Sharp Tool"
    <> wrote:

    >I use PrintWriter class:
    >
    >PrintWriter pw = new PrintWriter(new FileWriter("data.txt"));
    >pw.println(num);
    >pw.close();


    Thanks Sharp - I have got this to work by adding as follows - but is
    it better to put your code into a separate class? Beginner confusion!

    Cheers

    Geoff

    public void stateChanged(ChangeEvent event)
    {
    JSlider src = (JSlider) event.getSource();
    if(!src.getValueIsAdjusting())
    position.setText("Position is
    "+scale.getValue());

    try
    {
    FileWriter outfile = new FileWriter ("data.txt");
    PrintWriter pw = new PrintWriter(outfile);
    pw.println(scale.getValue());
    pw.close();
    }
    catch (IOException e) { System.out.println(e); }

    }





    >
    >Cheers
    >Sharp
    >
    >
    >"Geoff Cox" <> wrote in message
    >news:...
    >> Hello,
    >>
    >> I'm not clear how to write a vlaue to a file on the hard disk ... I
    >> have the code below and wish to write the getValue() number to disk.
    >>
    >> I am guessing that I need to call (with this value) a method to do
    >> this but my following effort is wrong. Please see also the rest of the
    >> code after my signature
    >>
    >> writeToFile(scale.getValue());
    >>
    >> }
    >>
    >> public static String writeToFile (int num)
    >> {
    >> try
    >> {
    >> FileWriter file = new FileWriter("data.txt");
    >> BufferedWriter buffer = new BufferedWriter(file);
    >> buffer.write(num);
    >> buffer.close();
    >> }
    >> catch(IOException e) { System.out.println(e); }
    >> }
    >>
    >> Help! please
    >>
    >> Geoff
    >>
    >> --------------------
    >>
    >> public void stateChanged(ChangeEvent event)
    >> {
    >> JSlider src = (JSlider) event.getSource();
    >> if(!src.getValueIsAdjusting())
    >> position.setText("Position is
    >> "+scale.getValue());
    >>
    >> writeToFile(scale.getValue());
    >>
    >> }
    >>
    >> public static String writeToFile (int num)
    >> {
    >> try
    >> {
    >> FileWriter file = new FileWriter("data.txt");
    >> BufferedWriter buffer = new BufferedWriter(file);
    >> buffer.write(num);
    >> buffer.close();
    >> }
    >> catch(IOException e) { System.out.println(e); }
    >> }

    >
     
    Geoff Cox, Jul 21, 2005
    #4
  5. Geoff Cox

    Geoff Cox Guest

    On Thu, 21 Jul 2005 10:05:27 +0200, Thomas Weidenfeller
    <> wrote:

    >Geoff Cox wrote:
    >> Hello,
    >>
    >> I'm not clear how to write a vlaue to a file on the hard disk ... I
    >> have the code below and wish to write the getValue() number to disk.

    >
    >http://java.sun.com/docs/books/tutorial/essential/io/index.html
    >
    >BTW, beginner questions are best asked in comp.lang.java.help


    OK - will do!

    Cheers

    Geoff

    >
    >/Thomas
     
    Geoff Cox, Jul 21, 2005
    #5
  6. Geoff Cox

    Sharp Tool Guest


    > >I use PrintWriter class:
    > >
    > >PrintWriter pw = new PrintWriter(new FileWriter("data.txt"));
    > >pw.println(num);
    > >pw.close();

    >
    > Thanks Sharp - I have got this to work by adding as follows - but is
    > it better to put your code into a separate class? Beginner confusion!
    >
    > Cheers
    >
    > Geoff


    No, it's not neccessary.

    Cheers
    Sharp
     
    Sharp Tool, Jul 21, 2005
    #6
    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. Lialie
    Replies:
    1
    Views:
    373
  2. scad
    Replies:
    4
    Views:
    979
    James Kanze
    May 28, 2009
  3. Iulian Ilea
    Replies:
    1
    Views:
    344
    pcx99
    Dec 21, 2006
  4. VUNETdotUS
    Replies:
    25
    Views:
    514
    Thomas 'PointedEars' Lahn
    Nov 10, 2007
  5. Tim Chase
    Replies:
    0
    Views:
    107
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page