-1

尝试创建两种计算编号的方法时。行并读取文件的值,只有其中一个方法被执行,而另一个未执行,显示以下错误:线程“main”中的异常 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 回答