Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Store a file as an integer

Reply
Thread Tools

Store a file as an integer

 
 
cyclone771@gmail.com
Guest
Posts: n/a
 
      10-01-2005
Hi. I have recently started learning to program using Java, and I am
currently working on creating a counter. Right now I am working as an
application only, and would like to know how to get my program to open
a file (Already done, I think.) and get it to save the contents as an
integer, x. Any help would be greatly appreciated. Here is the source
code:

import java.io.*;

class counter {
public static void main(String[] args) throws IOException {
File inputFile = new File("counter.jvar");
File outputFile = new File("counter.jvar");

FileReader in = new FileReader(inputFile);
int x = in;
/* I know this(^^) line creates an error b/c of diff file types, but
how do I fix it? */
in.close();
int y = (x + 1);
System.out.println(y);
FileWriter out = new FileWriter(outputFile);
out.write(y);
out.close();
}
}

 
Reply With Quote
 
 
 
 
Bruce Lee
Guest
Posts: n/a
 
      10-01-2005

<> wrote in message
news: oups.com...
> Hi. I have recently started learning to program using Java, and I am
> currently working on creating a counter. Right now I am working as an
> application only, and would like to know how to get my program to open
> a file (Already done, I think.) and get it to save the contents as an
> integer, x. Any help would be greatly appreciated. Here is the source
> code:
>
> import java.io.*;
>
> class counter {
> public static void main(String[] args) throws IOException {
> File inputFile = new File("counter.jvar");
> File outputFile = new File("counter.jvar");
>
> FileReader in = new FileReader(inputFile);
> int x = in;
> /* I know this(^^) line creates an error b/c of diff file types, but
> how do I fix it? */
> in.close();
> int y = (x + 1);
> System.out.println(y);
> FileWriter out = new FileWriter(outputFile);
> out.write(y);
> out.close();
> }
> }
>

You're better off using a FileInputStream and FileOutputStream for this.
The error is because you're saying the int x is a FileReader object which it
isn't - use the read() method instead. ie. int x = fileinputstream.read();


 
Reply With Quote
 
 
 
 
Malte
Guest
Posts: n/a
 
      10-01-2005
wrote:
> Hi. I have recently started learning to program using Java, and I am
> currently work

This

http://java.sun.com/docs/books/tutor.../io/index.html

should help you.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-01-2005
On 30 Sep 2005 21:03:27 -0700, wrote or quoted :

> Right now I am working as an
>application only, and would like to know how to get my program to open
>a file (Already done, I think.


see http://mindprod.com/applets/fileio.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-01-2005
On Sat, 01 Oct 2005 04:33:15 GMT, "Bruce Lee"
<> wrote or quoted :

>You're better off using a FileInputStream and FileOutputStream for this.
>The error is because you're saying the int x is a FileReader object which it
>isn't - use the read() method instead. ie. int x = fileinputstream.read();
>


for character i/o, a Reader/Writer is correct. Using an
InputStream/OutputStream is deprecated for that purpose because of the
problems with encoding.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
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 make integer to store 01 instead of 1 ? gopesh patel C++ 5 09-16-2010 12:53 PM
Store an array to an integer array?? rhitz1218@gmail.com C Programming 7 12-12-2006 12:43 AM
Store a char into an integer variable obdict C Programming 12 02-08-2006 11:39 PM
to store or not to store an image =?Utf-8?B?UnVkeQ==?= ASP .Net 6 03-30-2005 05:51 AM
Java Datatype to store integer more than 100 digits anand_ps Java 3 01-19-2005 04:06 AM



Advertisments