1

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

totScore, 设置为 25,当我在写入文件之前打印到控制台时,我看到25. 但是,当我读取文件并打印到控制台时,我得到三个值... 25、、1310。在记事本中查看文本文件给了我一个不在键盘上的字符,所以我怀疑该文件存储在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());
        }
    }
}
4

1 回答 1

4

您正在读取/写入字符串和原始数据,但不一致。为什么不改为读取字符串(使用某种 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();
            }
         }
      }
   }
}
于 2012-05-06T05:18:20.613 回答