-1

When trying to create two methods for counting the no. of rows and reading the values of a file, only one of these methods got executed and another is not executed showing the following error :Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Read error

Please look at the following code:

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);
        }


    }

         }
szegedi
  • 863
  • 8
  • 20
sandy444
  • 11
  • 2
  • 5
  • 6
    post you code here – Dangling Piyush Sep 07 '12 at 09:58
  • 1
    Have you closed your file in the first method before calling the second one? – Roman Rdgz Sep 07 '12 at 10:06
  • yes i have used .close() method in the first function – sandy444 Sep 07 '12 at 10:08
  • You can't use a stream after it has been closed. You may need to open a new stream to the same file or have one method which reads the file once and does both calculations. – Peter Lawrey Sep 07 '12 at 10:15
  • i used (DataInputStream strFile = new DataInputStream(fstream); BufferedReader br = new BufferedReader( new InputStreamReader (strFile));) in two methods for opening file , my aim is in first method i have to count row lines of file after that closing the file and in second method i have to read the values in the same file after that again closing the file. – sandy444 Sep 07 '12 at 10:27
  • @dangling and Amandeep ..... I posted my code – sandy444 Sep 07 '12 at 10:39
  • Please don't use DataInputStream to read text. Unfortunately examples like this get copied again and again so can you can remove it from your example. http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html – Peter Lawrey Jan 31 '13 at 00:07

2 Answers2

0

Just a small modification will do the work:

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
      }


    }

         }

Close all other streams as well. Above code will work for you.Cheers.

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52
  • when i am implementing ur solution i got following error :Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Read error at com.ibm.csvreader.CsvFileReader2.main(CsvFileReader2.java:87) Caused by: java.io.IOException: Read error at java.io.FileInputStream.read(FileInputStream.java:224) at java.io.DataInputStream.read(DataInputStream.java:143) at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:464) at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:506) – sandy444 Sep 07 '12 at 12:54
0

When you close your BufferedReader, it also closes the nested classes, including the FileInputStream.

Instead of closing it, you should try and reset() it to restart reading it from the start.

Or you must re-open the FileInputStream.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • when implementing ur solution i got following error:Exception in thread "main" java.lang.RuntimeException: java.io.IOException: Stream not marked at com.ibm.csvreader.CsvFileReader2.main(CsvFileReader2.java:83) Caused by: java.io.IOException: Stream not marked at java.io.BufferedReader.reset(BufferedReader.java:496) at com.ibm.csvreader.CsvFileReader2$opencsvfile.readfile(CsvFileReader2.java:68) at com.ibm.csvreader.CsvFileReader2.main(CsvFileReader2.java:80) – sandy444 Sep 07 '12 at 12:39