2016-11-02 8 views
0

次のプログラムを実行していて、ストリームクローズドIOエラーを取得しています。 ですが、2番目のループにのみあります。最初の1つは正常に動作します。 誰でも教えてください。 (私は、ファイルが空で、既存およびされていないことを確認。)ここでストリームクローズドIO例外Java

private static TimerTask perform(){ 
     //logging on to FTP-Server 

      InputStream in = client.retrieveFileStream("./htdocs/pwiMain/main.txt"); 
      InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt"); 
      BufferedInputStream inbf = new BufferedInputStream(in); 
      int bytesRead; 
      byte[] buffer = new byte[1024]; 
      String wholeFile = null; 
      String[] contents; 
      while((bytesRead = inbf.read(buffer)) != -1){ 
       wholeFile = new String(buffer,0,bytesRead); 
      } 
      sentPassword = wholeFile.substring(wholeFile.indexOf("#lap")); 
      inbf.close(); 

      inbf = new BufferedInputStream(pw); 
      while((bytesRead = inbf.read(buffer)) != -1){ // this is line72 where the error occurrs... 
       wholeFile = new String(buffer,0,bytesRead); 
      } 
      md5hash = wohleFile; 
      inbf.close(); 
      contents = sentPassword.split("\\r\\n|\\n|\\r"); 
      System.out.println("contents: " + contents[0] + " " + contents[1]); 

      //check the password 


     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      System.out.println("ioexception"); 
     } finally { 

     } 
     return null; 
    } 

はエラーメッセージです:あなたの助けのための

java.io.IOException: Stream closed 
    at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159) 
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) 
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) 
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345) 
    at java.io.FilterInputStream.read(FilterInputStream.java:107) 
    at com.protonmail.taylor.faebl.development.main.perform(main.java:72) 
    at com.protonmail.taylor.faebl.development.main.main(main.java:23) 

どうもありがとう:)

+0

2番目のファイルのファイルハンドルは、このメソッド以外の他の人が保持していますか(リソースは閉じていません)? –

+0

いいえこの目的でのみ使用され、パスワードチェック後に閉じる必要があります –

+1

client.retrieveFileStream()のコードを表示します。 –

答えて

2

あなたは明らかにできません同時に2つの検索ストリームをアクティブにします。これは驚くべきことではありません。ちょうどあなたのコードを並べ替える:あなたは今それをやっている方法にはポイントや利点がありません

private static TimerTask perform(){ 
    try { 
     //logging on to FTP-Server 

     InputStream in = client.retrieveFileStream("./htdocs/pwiMain/main.txt"); 
     BufferedInputStream inbf = new BufferedInputStream(in); 
     int bytesRead; 
     byte[] buffer = new byte[1024]; 
     String wholeFile = null; 
     String wholeCred = null; 
     String[] contents; 
     while((bytesRead = inbf.read(buffer)) != -1){ 
      wholeFile = new String(buffer,0,bytesRead); 
     } 
     inbf.close(); // ADDED 

     InputStream pw = client.retrieveFileStream("./htdocs/pwiMain/cred_pwd.txt"); 
     BufferedInputStream pwbf = new BufferedInputStream(pw); 
     int pwBytesRead; // YOU DON'T NEED THIS, you could reuse the previous one 
     byte[] pwBuffer = new byte [1024]; // DITTO 
     while((pwBytesRead = pwbf.read(pwBuffer)) != -1){ 
      wholeCred = new String(pwBuffer,0,pwBytesRead); 
     } 
     pwbf.close(); // ADDED 

     sentPassword = wholeFile.substring(sentPassword.indexOf("#lap")); 
     md5hash = wholeCred; 
     contents = sentPassword.split("\\r\\n|\\n|\\r"); 
     System.out.println("contents: " + contents[0] + " " + contents[1]); 

     //check the password 


    } catch (IOException e) { 
      // TODO Auto-generated catch block 
     e.printStackTrace(); 
     System.out.println("ioexception"); 
    } finally { 

    } 
    return null; 
} 

、あなただけのスペースを無駄にしている、とあなたはそれが動作しません発見したとして。

もちろん、どちらかの入力が1行を超えても機能しないが、それについては質問しなかったことがわかります。

+0

私は試しました(私はコードを更新しました) しかし、私はまだ同じエラーを受け取ります... (今は入力ストリームを再利用します) cred_pwd.txtファイルは1行だけです。 もう1つは複数の行で、コンテンツに割り当てられている場所で分割する必要があります(間違いですか?) –