Store a file as an integer

Discussion in 'Java' started by cyclone771@gmail.com, Oct 1, 2005.

  1. Guest

    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();
    }
    }
     
    , Oct 1, 2005
    #1
    1. Advertising

  2. Bruce Lee Guest

    <> wrote in message
    news:...
    > 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();
     
    Bruce Lee, Oct 1, 2005
    #2
    1. Advertising

  3. Malte Guest

    Malte, Oct 1, 2005
    #3
  4. Roedy Green Guest

    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.
     
    Roedy Green, Oct 1, 2005
    #4
  5. Roedy Green Guest

    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.
     
    Roedy Green, Oct 1, 2005
    #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. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    666
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  2. anand_ps
    Replies:
    3
    Views:
    1,635
    Ryan Stewart
    Jan 19, 2005
  3. obdict

    Store a char into an integer variable

    obdict, Feb 8, 2006, in forum: C Programming
    Replies:
    12
    Views:
    558
    Mark McIntyre
    Feb 8, 2006
  4. Replies:
    7
    Views:
    274
    Barry Schwarz
    Dec 12, 2006
  5. gopesh patel
    Replies:
    5
    Views:
    2,226
    Stuart Golodetz
    Sep 16, 2010
Loading...

Share This Page