Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7890201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:17:36+00:00 2026-06-03T06:17:36+00:00

I’ve got a program where I need to generate an integer, write it to

  • 0

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());
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:17:38+00:00Added an answer on June 3, 2026 at 6:17 am

    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();
                }
             }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I have a reasonable size flat file database of text documents mostly saved in
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.