2017-07-06 2 views
-1

起動時に私のプログラムは私にメッセージjava.io.IOExceptionを与えます:ストリームが閉じました。それから、制限されたコンポーネントで開きます。ioexceptionストリームを終了する方法を修正しましたか?

私は、ストリームが例外を閉鎖していることを理解していますが、私の場合、修正方法を理解できません。私は本当に助けに感謝します。 (私は、コードの大きな塊について申し訳ありませんが、私はあなたが私を助けるために必要なものをわからない)私は何を見つけるのですが、私は)(br1.closeを削除していたときにストリームが閉じられていることである

try { 
    //Defines new file reader and buffered reader 
    FileReader file_to_read = new FileReader(path); 
    BufferedReader br1 = new BufferedReader(file_to_read); 

    //action code 

     try { 
     //Finds the line number to see the number of cards in each set 
     lnr2= new LineNumberReader(new FileReader(new File("C:\\Users\\priceadria\\Desktop\\Words_" + strSetName + ".txt"))); 
     lnr2.skip(Long.MAX_VALUE); 
     strLineNumber2 = String.valueOf(lnr2.getLineNumber()); 
     lineNumber2 = Integer.parseInt(strLineNumber2); 

     //Closes the file reader and buffered reader 
     br1.close(); 
     file_to_read.close(); 

    } catch (IOException | NumberFormatException e) { 
     strLineNumber2 = "0"; 
    }   
     //Adds the set name to the set name array 
     lblDeckName[row] = new JLabel (strSetName); 

     //Adds all buttons that will be used in the layout into a button array 
     JButton aryOptions [][] = new JButton [3][lineNumber]; 

     //Defines and formats the Add Words button 
     JButton btnAddWords = new JButton("Add Words"); 
     btnAddWords.setOpaque(false); 
     btnAddWords.setContentAreaFilled(false); 
     btnAddWords.setBorder(null); 
     btnAddWords.setBorderPainted(false); 

     //Defines and formats the Play button 
     JButton btnPlay = new JButton("Play"); 
     btnPlay.setOpaque(false); 
     btnPlay.setContentAreaFilled(false); 
     btnPlay.setBorder(null); 
     btnPlay.setBorderPainted(false); 

     //Defines and formats the Delete Set button 
     JButton btnDelete = new JButton("Delete Set"); 
     btnDelete.setOpaque(false); 
     btnDelete.setContentAreaFilled(false); 
     btnDelete.setBorder(null); 
     btnDelete.setBorderPainted(false); 

     //Adds each button to the array based on the number of sets 
     aryOptions [0][row] = btnAddWords; 
     aryOptions [1][row] = btnPlay; 
     aryOptions [2][row] = btnDelete; 

     //Formats the GridBagLayout (Spaces are for formatting purposes) 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 0; 
     gbc.gridy = row; 
     layoutBackground.add(lblDeckName[row],gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 1; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel("  "),gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx =2; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel (strSubjectName),gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 3; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel("     "),gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 4; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel(strLineNumber2),gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 5; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel("    | "),gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 6; 
     gbc.gridy = row; 
     layoutBackground.add(aryOptions[0][row],gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 7; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel(" | "),gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 8; 
     gbc.gridy = row; 
     layoutBackground.add(aryOptions[1][row],gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 9; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel(" | "),gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 10; 
     gbc.gridy = row; 
     layoutBackground.add(aryOptions[2][row],gbc); 

     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.gridx = 11; 
     gbc.gridy = row; 
     layoutBackground.add(new JLabel(" |"),gbc); 




     //Creates an action event for the delete set button 
     btnDelete.addActionListener((ActionEvent a) -> { 
      //Finds the source of the button that was clicked 
      String strSource = String.valueOf(a.getSource()); 
      String strSourceCut1 = strSource.substring(25,30); 

      //Finds the index of the button that was clicked 
      String [] aryIndex = strSourceCut1.split(","); 
      String strIndex = aryIndex[0]; 

      try { 
       //Defines new file reader and buffered reader     
       FileReader file_to_read2 = new FileReader(path); 
       BufferedReader br2 = new BufferedReader(file_to_read2); 

       //Finds the numerical index of the button that was clicked 
       int intIndex = Integer.parseInt(strIndex)/16; 

       //Creates a counter to count line numbers 
       int counter = 0; 

       while((br2.readLine()) != null) { 

        counter ++; 
        if(intIndex == 0) { //If the index is the first option the normal try/catch returns null pointer, so a new try/catch had to be created 
         try { 
          //Finds the name of the set based on what button was clicked 
          String chosenDeck2 = br2.readLine(); 
          String [] aryDeleteDeck = chosenDeck2.split(","); 
          deleteDeck = aryDeleteDeck[0]; 

          //Closes the buffered reader and file reader 
          br2.close(); 
          file_to_read2.close(); 

          //Creates the path to the file that needs to be deleted 
          Path p2 = Paths.get("C:\\Users\\priceadria\\Desktop\\Words_" + deleteDeck + ".txt"); 
          Files.delete(p2); 

         } catch (IOException e) { 
          JOptionPane.showMessageDialog(null, e); 
         } 

        } else if (counter == intIndex) { 
         //Reads the deck name based on the button that was clicked 
         String chosenDeck2 = br2.readLine(); 

         //Splits the deck name from the subject 
         String [] aryDeleteDeck = chosenDeck2.split(",");    
         deleteDeck = aryDeleteDeck[0]; 

         //Closes the buffered reader and file reader 
         br2.close(); 
         file_to_read2.close(); 

         //Gets the file that needs to be deleted 
         Path p1 = Paths.get("C:\\Users\\priceadria\\Desktop\\Words_" + deleteDeck + ".txt"); 
         Files.delete(p1); 

        } 
       } 
      } 
      catch (HeadlessException | IOException | NumberFormatException e) { 
       JOptionPane.showMessageDialog(null, e); 
      } 
     }); 

     //Creates an action event for the add words button 
     btnAddWords.addActionListener((ActionEvent a) -> { 
      //Finds the source of the button that was clicked 
      String strSource = String.valueOf(a.getSource()); 
      String strSourceCut1 = strSource.substring(25,30); 

      //Finds the index of the button that was clicked 
      String [] aryIndex = strSourceCut1.split(","); 
      String strIndex = aryIndex[0]; 

      try { 
       //Creates a new file reader and buffered reader 
       FileReader file_to_read3 = new FileReader(path); 
       BufferedReader br3 = new BufferedReader(file_to_read3); 

       //Finds the index value 
       int intIndex = Integer.parseInt(strIndex)/16; 

       //Creates a counter to count line numbers 
       int counter = 0; 

       while((br3.readLine()) != null) { 
        //Increases counter value by 1 
        counter ++; 
        if(intIndex == 0) 
        { 
         try { 
          //Reads the set and subject for the 
          String chosenDeck2 = br3.readLine(); 

          //Splits the subject from the set 
          String [] aryDeck = chosenDeck2.split(","); 
          chosenDeck = aryDeck[0]; 

          //Closes the buffered reader and file reader 
          br3.close(); 
          file_to_read3.close(); 



         } catch (Exception e) { 
          JOptionPane.showMessageDialog(null, e); 
         } 

        } else if (counter == intIndex) { 
         String chosenDeck2 = br3.readLine(); 

         String [] aryDeck = chosenDeck2.split(","); 
         chosenDeck = aryDeck[0]; 

         br3.close(); 
         file_to_read3.close(); 
        } 

       } 
      } 
      catch (Exception e) { 
       JOptionPane.showMessageDialog(null, e); 
      } 
      //Opens the new words frame 
      frmWords s = new frmWords(deckName, newXPoint, newYPoint, chosenDeck); 
      s.setVisible(true); 
     }); 
    } 

    } catch (Exception e) { 
     JOptionPane.showMessageDialog(null, e); 


    } 

エラーは止まりますが、理由は何ですか?私が見ることができるその読者を使用しているものは何もありません。そして私がそれを削除すると、テストファイルが他のプロセスによって使用されているというエラーが表示されます。これは私が想定できるのはbr1だけです。

私はこれが見苦しいかもしれないことを知っています。おそらくそれはコードが不適切だからかもしれませんが、本当に助けに感謝します。私は高校生です。

感謝:)

+0

whileループでbr2を終了しているようです。これが複数回実行されるかどうかによって、これは問題になる可能性があります。 – MaxPower

+0

br3と同じです。 – MaxPower

+0

こちらをご覧ください。https://stackoverflow.com/questions/22900477/java-io-exception-stream-closed コードを関数に分割し、学習後に問題を再現するために最小限のコードで再現しようとすると、利益を得ることができます。 – Omrisk

答えて

0

あなたは、Java 7からclosable機能を使用することができます。

try (FileReader file_to_read = new FileReader(path); 
       BufferedReader br1 = new BufferedReader(file_to_read)){ 
      //Finds the line number to see the number of cards in each set 
      lnr2= new LineNumberReader(new FileReader(new File("C:\\Users\\priceadria\\Desktop\\Words_" + strSetName + ".txt"))); 
      lnr2.skip(Long.MAX_VALUE); 
      strLineNumber2 = String.valueOf(lnr2.getLineNumber()); 
      lineNumber2 = Integer.parseInt(strLineNumber2); 
     } catch (IOException | NumberFormatException e) { 
      strLineNumber2 = "0"; 
     } 

は、その後、あなたは正しい順序であなたのストリームやファイルを閉じるを気にする必要はありません。
さらに、apache commonsやguavaのような "readFile"ジョブを実行するためにlibsを使うこともできます。

0

あなたがそうであるように、あなたがネストされたReadersを構築し、例えば、

FileReader file_to_read = new FileReader(path); 
    BufferedReader br1 = new BufferedReader(file_to_read); 

、自動的にあまりにも、内側の1を閉じ、外側1を閉じます。内側のものを自分で閉じる必要はありません。外側のものを閉じた後にそうしようとすると、すでに閉じていることがわかります。

最良の解決策は、外側のものだけを閉じることです(内部のものを閉じるという回避策ではありません)。それはあなたの空想に合った場合

BufferedReader br1 = new BufferedReader(new FileReader(path)); 

    try { 
     // ... manipulate br1 ... 
    } finally { 
     br1.close(); 
     // no need to explicitly close the inner FileReader 
    } 

あなたはその中のtry-と資源のフォームを作成することができます:このような何かを行くと、より一般的なイディオム - あなたはおそらく、内部1への参照を保持する必要はありません。 。

同様にネストされたWriters、InputStreams、およびOutputStreamsにも適用されます。

関連する問題