1

整数を生成し、それをテキスト ファイルに書き込んで、次にプログラムを実行するときに読み戻す必要があるプログラムがあります。いくつかの異常な動作の後、整数値を設定するようにそれを取り除き、ファイルに書き込み、デバッグのために読み戻しました。

totScoreが 25 に設定されており、ファイルに書き込む前にコンソールに出力すると、値が25. ただし、ファイルを読み取ってコンソールに出力すると、 、 、 の 3 つの値が得25られ13ます10。メモ帳でテキスト ファイルを表示すると、キーボードにない文字が表示されるので、ファイルが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

文字列と生データを読み書きしていますが、一貫性がありません。代わりに Strings を (ある種の Reader を使用して) 読み込んでから、String を解析して 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 に答える