Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how write value to file?

Reply
Thread Tools

how write value to file?

 
 
Geoff Cox
Guest
Posts: n/a
 
      07-21-2005
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); }
}
 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      07-21-2005
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/tutor.../io/index.html

BTW, beginner questions are best asked in comp.lang.java.help

/Thomas
 
Reply With Quote
 
 
 
 
Sharp Tool
Guest
Posts: n/a
 
      07-21-2005
I use PrintWriter class:

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

Cheers
Sharp


"Geoff Cox" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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); }
> }



 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-21-2005
On Thu, 21 Jul 2005 08:14:28 GMT, "Sharp Tool"
<(E-Mail Removed)> 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" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> 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); }
>> }

>


 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-21-2005
On Thu, 21 Jul 2005 10:05:27 +0200, Thomas Weidenfeller
<(E-Mail Removed)> 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/tutor.../io/index.html
>
>BTW, beginner questions are best asked in comp.lang.java.help


OK - will do!

Cheers

Geoff

>
>/Thomas


 
Reply With Quote
 
Sharp Tool
Guest
Posts: n/a
 
      07-21-2005

> >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




 
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
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM
DVD Write speed - Not able to write at 8 speed Vincent Wonnacott Computer Support 1 09-16-2004 03:14 PM
write a union object using write JasBascom C++ 1 02-24-2004 08:18 AM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM
How to open a new console and write something out write printf() Peter Hansen C Programming 1 08-24-2003 11:49 AM



Advertisments