Reading single InputStream from multiple methods

后端 未结 3 432
轮回少年
轮回少年 2021-01-26 18:21

I have initialized an InputStream in a single method in a class and passing it to next method for processing. The InputStream essentially encapsulates CSV file for processing.

3条回答
  •  离开以前
    2021-01-26 18:54

    If you need a BufferedReader, I think you need to create it in in main method:

    main() {
      FileInputStream fis = new FileInputStream("FileName.CSV");
      BufferedInputStream bis = new BufferedInputStream(fis);
      InputStreamReader isr = new InputStreamReader(bis);
      BufferedReader br = new BufferedReader(isr);
    
      processCSV(br);
    }
    
    processCSV(Reader isr) {
      fetchHeaders(isr);
      processContentRows(isr);
    }
    
    fetchHeaders(Reader isr) {
      //Use BufferedReader to retrieve first line of CSV
      //Even tried mark() and reset() here
    }
    
    processContentRows(Reader isr) {
      //Cannot read the values, fetches null from InputStream :(
    }
    

提交回复
热议问题