【问题标题】:Why does the integer written to a file get read as a different value?为什么写入文件的整数被读取为不同的值?
【发布时间】:2012-05-06 05:16:45
【问题描述】:

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

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

【问题讨论】:

  • 您正在使用字符 I/O API (FileWriter) 进行写入,并使用字节 I/O (FileInputStream) 进行读取。问题是由于写作和阅读方法的不匹配造成的。

标签: java file-io integer


【解决方案1】:

您正在读取/写入字符串和原始数据,但不一致。为什么不改为读取字符串(使用某种 Reader),然后通过解析字符串转换为 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();
            }
         }
      }
   }
}

【讨论】:

  • Fullof Eels - 我是 i/o 新手...bw.write(totScore); 是否将我的 int 转换为 String?当我阅读文件时,使用BufferedReader/InputStreamReader 会给我一个String 吗?
  • @dwwilson:请参阅编辑以了解解决此问题的两种相反方法。
  • @dwwilson:太好了,不客气!很高兴看到迫使我思考的问题,非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 2021-05-29
  • 2023-03-06
  • 1970-01-01
  • 2020-06-26
相关资源
最近更新 更多