dwwilson66
dwwilson66

Reputation: 7076

Why does the integer written to a file get read as a different value?

I've got a program where I need to generate an integer, write it to a text file and read it back the next time the program runs. After some anomalous behavior, I've stripped it down to setting an integer value, writing it to a file and reading it back for debugging.

totScore, is set to 25 and when I print to the console prior to writing to the file, I see a value of 25. However, when I read the file and print to the console I get three values...25, 13, and 10. Viewing the text file in notepad gives me a character not on the keyboard, so I suspect that the file is being stored in something other that int.

Why do I get different results from my write and read steps?

Is it not being written as an int? How are these values being stored in the file? Do I need to cast the read value as something else and convert it to an integer?

Consider:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.nio.file.StandardOpenOption.*;
//
public class HedgeScore  {
    public static void main(String[] args)  {
        int totScore = 25;
        OutputStream outStream = null;   ///write
        try {
            System.out.println("totscore="+totScore);
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File("hedgescore.txt")));
            bw.write(totScore);
            bw.write(System.getProperty("line.separator"));
            bw.flush();
            bw.close();
        }
        catch(IOException f)  {
            System.out.println(f.getMessage());
        }
        try  {
        InputStream input = new FileInputStream("hedgescore.txt");
            int data = input.read();
            while(data != -1)  {
                System.out.println("data being read from file :"+ data);
                data = input.read();
                int prevScore = data;
            }
            input.close();
        }
        catch(IOException f)  {
            System.out.println(f.getMessage());
        }
    }
}

Upvotes: 1

Views: 504

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're reading/writing Strings and raw data, but not being consistent. Why not instead read in Strings (using a Reader of some sort) and then convert to int by parsing the String? Either that or write out your data as bytes and read it in as bytes -- although that can get quite tricky if the file must deal with different types of data.

So either:

import java.io.*;

public class HedgeScore {
   private static final String FILE_PATH = "hedgescore.txt";

   public static void main(String[] args) {
      int totScore = 25;
      BufferedWriter bw = null;
      try {
         System.out.println("totscore=" + totScore);
         bw = new BufferedWriter(new FileWriter(new File(
               FILE_PATH)));
         bw.write(totScore);
         bw.write(System.getProperty("line.separator"));
         bw.flush();
      } catch (IOException f) {
         System.out.println(f.getMessage());
      } finally {
         if (bw != null) {
            try {
               bw.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }

      InputStream input = null;
      try {
         input = new FileInputStream(FILE_PATH);
         int data = 0;
         while ((data = input.read()) != -1) {
            System.out.println("data being read from file :" + data);
         }
         input.close();
      } catch (IOException f) {
         System.out.println(f.getMessage());
      } finally {
         if (input != null) {
            try {
               input.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}

or:

import java.io.*;

public class HedgeScore2 {
   private static final String FILE_PATH = "hedgescore.txt";

   public static void main(String[] args) {
      int totScore = 25;
      PrintWriter pw = null;
      try {
         System.out.println("totscore=" + totScore);
         pw = new PrintWriter(new FileWriter(new File(FILE_PATH)));
         pw.write(String.valueOf(totScore));
         pw.write(System.getProperty("line.separator"));
         pw.flush();
      } catch (IOException f) {
         System.out.println(f.getMessage());
      } finally {
         if (pw != null) {
            pw.close();
         }
      }

      BufferedReader reader = null;
      try {
         reader = new BufferedReader(new FileReader(FILE_PATH));
         String line = null;
         while ((line = reader.readLine()) != null) {
            System.out.println(line);
         }
      } catch (IOException f) {
         System.out.println(f.getMessage());
      } finally {
         if (reader != null) {
            try {
               reader.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}

Upvotes: 4

Related Questions