2012-05-06 1 views
1

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

totScoreは25に設定されています。ファイルに書き込む前にコンソールに印刷すると、25の値が表示されます。しかし、ファイルを読み込んでコンソールにプリントすると、3つの値、つまり2513、および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()); 
     } 
    } 
} 
+0

あなたは文字I/OのAPI( 'FileWriter')を使用して書き込み、バイトI/O(' FileInputStream'を使用して読んでいます)。この問題は、この書き込みと読み取りの方法の不一致によるものです。 – dasblinkenlight

答えて

4

/文字列と生データを書き込むことが、一貫されていません。代わりにStringを読み込み(何らかの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(); 
      } 
     } 
     } 
    } 
} 
+0

Fullof Eels - 私はI/Oを使い慣れていないので、 'bw.write(totScore);'私の 'int'を' String'に変換しますか?私がファイルを読むとき 'BufferedReader' /' InputStreamReader'を使って 'String'を渡しますか? – dwwilson66

+0

@dwwilson:これを解決するには、2つの逆の方法を編集してください。 –

+1

素晴らしい!ありがとう!これは多くの助けになります! – dwwilson66

関連する問題