Java 项目在readFile方法调用后引发IOException
我目前正在编写一个程序,它将读取一个CSV文件,其中包含有关年龄、身高和体重的数据。我的问题是,当我调用从main读取文件的方法时,它抛出一个Java 项目在readFile方法调用后引发IOException,java,ioexception,Java,Ioexception,我目前正在编写一个程序,它将读取一个CSV文件,其中包含有关年龄、身高和体重的数据。我的问题是,当我调用从main读取文件的方法时,它抛出一个IOException。我不确定是什么导致了这种情况,因为这是针对一个项目的,我不允许从main()方法抛出IOException。我插入了一个输出打印行,试图找出错误发生的地方,因为eclipse中的调试模式不允许我这样做,而且错误似乎发生在readFile()方法启动之前 我对java和代码非常生疏,在长时间不接触编码之后,我开始学习这门课程。我不确定
IOException
。我不确定是什么导致了这种情况,因为这是针对一个项目的,我不允许从main()
方法抛出IOException
。我插入了一个输出打印行,试图找出错误发生的地方,因为eclipse中的调试模式不允许我这样做,而且错误似乎发生在readFile()
方法启动之前
我对java和代码非常生疏,在长时间不接触编码之后,我开始学习这门课程。我不确定从何处开始尝试并调试此IOException
public class Project1 {
public static void main(String[] args) {
//main method declaring project object
Project1 project = new Project1();
String fileName = project.checkArgs(args);
File blah = null; //temporary name for the file don't pay attention to it
try {
blah = project.getFile(fileName); //gets the file and sets it to variable in main
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
project.readFile(blah, 500); //trying to read in file of 2d array in the readFileMethod
}
catch (IOException i) { //method gets caught here
System.out.println("IOException");
}
}
//readFile METHOD
public String[][] readFile(File file, int numRecords) throws IOException {
//two parameters: file, being read in this method, and numRecords the amount
//of elements in the array (I haven't done anything with numRecords yet and I
//believe this may possibly be the source of the error
Scanner reader = new Scanner(file); //scanner to read in file
//2d array to be passed back to main
String[][] data = new String[numRecords][3];
System.out.println("hi"); //test for IOException, doesn't get to this point
int iteration = 0; //to skip first line of text in csv file
while (reader.hasNextLine()) { //loading in elements to 2d array
iteration++;
if (iteration < 1) {
reader.nextLine();
continue;
}
else {
String list[] = reader.nextLine().trim().split(",");
for (int i = 0; i < numRecords; i++) {
for (int j = 0; j < 3; j++) {
data[i][j] = list[j];
System.out.println(data[i][j]);
}
}
}
}
return data;
}
公共类项目1{
公共静态void main(字符串[]args){
//声明项目对象的主方法
Project1项目=新项目1();
字符串文件名=project.checkArgs(args);
File blah=null;//文件的临时名称不需要注意
试一试{
blah=project.getFile(fileName);//获取文件并将其设置为main中的变量
}
catch(filenotfound异常){
e、 printStackTrace();
}
试一试{
project.readFile(blah,500);//尝试在readFileMethod中读入2d数组的文件
}
catch(IOException i){//method在这里被捕获
System.out.println(“IOException”);
}
}
//readFile方法
公共字符串[][]读取文件(文件文件,int numRecords)引发IOException{
//两个参数:file(在此方法中读取)和numRecords(数量)
//数组中的元素(我还没有用numRecords做任何事情,我
//相信这可能是错误的根源
扫描仪阅读器=新扫描仪(文件);//要在文件中读取的扫描仪
//要传回主服务器的2d阵列
字符串[][]数据=新字符串[numRecords][3];
System.out.println(“hi”);//IOException的测试没有达到这一点
int iteration=0;//跳过csv文件中的第一行文本
而(reader.hasNextLine()){//将元素加载到2d数组
迭代++;
if(迭代<1){
reader.nextLine();
继续;
}
否则{
字符串列表[]=reader.nextLine().trim().split(“,”);
for(int i=0;i
程序在方法
main()
中捕获了IOException
,我找不到原因,请提供堆栈跟踪(I.printStackTrace()
)
由于不使用扫描仪的分隔符,您可以尝试其他方法:
BufferedReader br = new BufferedReader(new FileReader(file), bufferSize);
String line;
if ((line = br.readLine()) != null) {
// process the line
}
编辑您的问题并包括整个堆栈跟踪(作为文本,而不是图像)。