2012-05-03 279 views
1

MurachのSe 6の例を使用してファイル入出力を始めたところです。 ここに私のコードがあります。私は何かが欠けていますか?私はさらにコードがあることを知っているが、これは正しく動作するはずの例ですから?ファイルI/Oのエラー

//Import import java.io.*; for use with the File I/O Methods. 
import java.io.*; 
public class MainApp 
{ 

    public static void main(String[] args) 
    { 
    //Create a file object. 
    File productFile = new File("product.txt"); 
    //Open a buffered output stream to allow write to file operations. 
    PrintWriter out = new PrintWriter(
         new BufferedWriter(
         new FileWriter(productFile)));  

    out.println("java\tMurach's Beginning Java 2\t$49.99"); 
    out.close(); 

    BufferedReader in = new BufferedReader(
         new FileReader(productFile)); 

    String line = in.readLine(); 
    System.out.println(line); 

    out.close(); 


    } 

} 

//回答私は、このコードが動作するメインの初期化場所の最後に例外がスローされます追加して

。 txtファイルのproducts.txtも期待通りにクラスフォルダにあります。 //インポートはjava.io. *をインポートします。ファイルI/Oメソッドで使用するために使用します。

import java.io.*; 
public class MainApp 
{ 

    public static void main(String[] args) throws Exception 
    { 
    //Create a file object. 
    File productFile = new File("product.txt"); 
    //Open a buffered output stream to allow write to file operations. 
    PrintWriter out = new PrintWriter(
         new BufferedWriter(
         new FileWriter(productFile)));  

    out.println("java\tMurach's Beginning Java 2\t$49.99"); 
    out.close(); 

    BufferedReader in = new BufferedReader(
         new FileReader(productFile)); 

    String line = in.readLine(); 
    System.out.println(line); 

    out.close(); 


    } 

} 
+3

への呼び出しである必要があります。outは閉じたtwise – neohope

+3

あなたは何が間違っているかを伝える必要があります。 –

+0

FileReaderとFileWriterの両方に下線が引かれ、例外がスローされます。 – Pendo826

答えて

1

問題は、java.ioパッケージの呼び出しの多くが例外をスローすることです。

簡単に修正:あなたのメソッドのシグネチャに

public static void main(String[] args) throws IOException 

ほとんど同じくらい簡単に修正を次の行を追加しますのtry/catch/finallyブロックを追加します。

public static void main(String[] args) 
{ 
    //Create a file object. 
    File productFile = new File("product.txt"); 

    //Open a buffered output stream to allow write to file operations. 
    PrintWriter out = null; 
    try { 
     out = new PrintWriter(
       new BufferedWriter(
         new FileWriter(productFile)));  

     out.println("java\tMurach's Beginning Java 2\t$49.99"); 
    } 
    catch(IOException ex) { 
     // todo exception handling 

     System.out.println("ERROR! " + ex); 

    } 
    finally { 
     out.close(); 

    } 

    BufferedReader in = null; 
    try { 
     in = new BufferedReader(
       new FileReader(productFile)); 

     String line = in.readLine(); 
     System.out.println(line); 
    } 
    catch (IOException ex) { 
     // todo more exception handling 
     System.out.println("ERROR! " + ex); 
    } 
    finally { 
     in.close(); 
    } 

} 

編集:out.close()を2回呼び出そうとしていますか? 2番目はin.close()

+0

にはout.close()があります。 2回、あなたの提案の助けを借りて最終製品が例外を投げます。 – Pendo826

+0

私はout.close()が2回あなたの本の誤字を持っていると思います! –

+0

あなたはそうかもしれません。それでも出力には影響しません。私はバッファリングされた読者のためにストリームを再開することを前提としており、シャットダウンのようなものとしてもう一度閉じる必要がある。 – Pendo826

関連する問題