-1

いいえを数えるための2つの方法を作成しようとすると。行の数とファイルの値を読み取ると、これらのメソッドの 1 つだけが実行され、別のメソッドは実行されず、次のエラーが表示されます: スレッド「メイン」での例外 java.lang.RuntimeException: java.io.IOException: 読み取りエラー

次のコードを見てください。

package com.ibm.csvreader;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;

public class CsvFileReader2  {

    public static class opencsvfile {

        HashMap <String , String> map= new HashMap <String, String> ();
        //csv file containing data
         // FileReader  strFile = new FileReader("C:/Users/vmuser/Desktop/SampleUpload.csv");
          //create BufferedReader to read csv file

         // BufferedReader br = new BufferedReader((strFile));
          String strLine = "";
          int lineNumber ; 
         public void  countrows(FileInputStream fstream) throws Exception{
             DataInputStream strFile  = new DataInputStream(fstream);
             BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
              lineNumber =0;
             while( (strLine = br.readLine()) != null) {
                  lineNumber++;
                }
             System.out.println("no.of rows are :" +lineNumber);
             br.close();
         }
             public void readfile(FileInputStream fstream) throws Exception{
                 DataInputStream strFile  = new DataInputStream(fstream);
                 BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
                 lineNumber =0;
                 while( (strLine = br.readLine()) != null) {
                      lineNumber++;
                      String[] tokens = strLine.split(","); 
                      String key = tokens[0].trim(); 
                      String nodes = tokens[1].trim(); 
                      map.put(key, nodes);

                             }
                 System.out.println("map is" + map );
                 br.close();
                  System.out.println("File is Closed");
         }
    }
    public static void main(String[] args) throws IOException {
        File fl = new File ("C:/Users/vmuser/Desktop/SampleUpload.csv");
        FileInputStream fstream = new FileInputStream(fl);

         opencsvfile f=new opencsvfile();
         try {
            f.countrows(fstream);

             f.readfile(fstream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

         }
4

2 に答える 2

0

少し変更するだけで作業が完了します。

package com.ibm.csvreader;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;

public class CsvFileReader2  {



   public static class opencsvfile {

        HashMap <String , String> map= new HashMap <String, String> ();
        //csv file containing data
         // FileReader  strFile = new FileReader("C:/Users/vmuser/Desktop/SampleUpload.csv");
          //create BufferedReader to read csv file

         // BufferedReader br = new BufferedReader((strFile));
          String strLine = "";
          int lineNumber ; 
         public void  countrows(FileInputStream fstream) throws Exception{
             DataInputStream strFile  = new DataInputStream(fstream);
             BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
              lineNumber =0;
             while( (strLine = br.readLine()) != null) {
                  lineNumber++;
                }
             System.out.println("no.of rows are :" +lineNumber);
             br.close();
         }
             public void readfile(FileInputStream fstream) throws Exception{
                 DataInputStream strFile  = new DataInputStream(fstream);
                 BufferedReader br = new BufferedReader( new InputStreamReader (strFile));
                 lineNumber =0;
                 while( (strLine = br.readLine()) != null) {
                      lineNumber++;
                      String[] tokens = strLine.split(","); 
                      String key = tokens[0].trim(); 
                      String nodes = tokens[1].trim(); 
                      map.put(key, nodes);

                             }
                 System.out.println("map is" + map );
                 br.close();
                  System.out.println("File is Closed");
         }
    }
    public static void main(String[] args) throws IOException {
        File fl = new File ("C:/Users/vmuser/Desktop/SampleUpload.csv");
        FileInputStream fstream = new FileInputStream(fl);

         opencsvfile f=new opencsvfile();
         try {
            f.countrows(fstream);

           fstream = new FileInputStream(fl);//include this line

             f.readfile(fstream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
       finally{
          if(fstream!=null)
                fstream.close();//be sure to close all streams at last
      }


    }

         }

他のすべてのストリームも閉じます。上記のコードはあなたのために働くでしょう.乾杯.

于 2012-09-07T10:56:45.070 に答える
0

BufferedReader を閉じると、FileInputStream を含むネストされたクラスも閉じられます。

閉じるのではなく、reset() して最初から読み取りを再開する必要があります。

または、FileInputStream を再度開く必要があります。

于 2012-09-07T10:57:45.237 に答える