java - 为什么写入文件的整数会被读取为不同的值?

标签 java file-io integer

我有一个程序,我需要在其中生成一个整数,将其写入文本文件并在程序下次运行时读回。在出现一些异常行为后,我将其简化为设置一个整数值,将其写入文件并读回以进行调试。

totScore 设置为 25,当我在写入文件之前打印到控制台时,我看到了 25 的值。但是,当我读取文件并打印到控制台时,我得到了三个值...251310。在记事本中查看文本文件时,我得到了一个不在键盘上的字符,因此我怀疑该文件存储在 int 之外的其他地方。

为什么我的写入和读取步骤会得到不同的结果?

它不是被写成一个int吗?这些值如何存储在文件中?我是否需要将读取的值转换为其他值并将其转换为整数?

考虑:

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());
        }
    }
}

最佳答案

您正在读/写字符串和原始数据,但不一致。为什么不改为读入字符串(使用某种阅读器),然后通过解析字符串转换为 int?或者,或者将您的数据作为字节写出并作为字节读入——尽管如果文件必须处理不同类型的数据,这可能会变得非常棘手。

所以要么:

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();
            }
         }
      }
   }
}

或:

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();
            }
         }
      }
   }
}

关于java - 为什么写入文件的整数会被读取为不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468309/

相关文章:

java - 我怎样才能让我的测试版过期?

java - 以编程方式排列 aem 6.4 中的内容

java - List<Long> 如何引用具有 BigInteger 值的 ArrayList

java - Java有路径连接方法吗?

javascript - 在 javascript 应用程序中管理本地文件 I/O 的最便捷方式

c++ - 如何使用 cout << myclass

java - 将 .class 文件添加到 NetBeans 中的项目

c - 将txt文件的结构加载到动态数组

debugging - Visual Studio 调试器 - 以十六进制显示整数值

swift - 将整数四舍五入为 3 的倍数