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