为什么InputStream istream = this.getClass().getResourceAsStream("/isrs.csv")返回的是null?
public void run() {
// 创建reader对象
CSVReader reader = null;
try {
// 创建输入流
final InputStream istream = this.getClass().getResourceAsStream("/isrs.csv");
if (istream == null) {
System.out.println("Cannot access data set, make sure the resources are available.");
System.exit(1);
}
// 如果输入流不为空,初始化reader对象
reader = new CSVReader(new InputStreamReader(istream));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(Arrays.toString(nextLine));
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 释放资源
// 如果reader不为空,则关闭
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
是不是存放文件的路径不对,我这里在eclipse下测试,发现需要将文件存放在eclise的bin目录下的某个工程目录下才可以。
测试如下:
参考链接:
https://www.cnblogs.com/macwhirr/p/8116583.html
https://mvnrepository.com/artifact/com.opencsv/opencsv
https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
https://mvnrepository.com/artifact/org.apache.commons/commons-text
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils
package com.csv.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
public class TestReadCsv {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestReadCsv trc = new TestReadCsv();
try {
trc.run();
} catch (CsvValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() throws CsvValidationException {
// https://mvnrepository.com/artifact/com.opencsv/opencsv
// https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
// https://mvnrepository.com/artifact/org.apache.commons/commons-text
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
// https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils
// https://zhuanlan.zhihu.com/p/343069880
// 创建reader对象
CSVReader reader = null;
try {
// https://www.cnblogs.com/macwhirr/p/8116583.html
// 在eclipse中测试,需要是放在bin目录的某个工程目录下,我这里是使用"/com/csv"工程目录
// 创建输入流
final InputStream istream = this.getClass().getResourceAsStream("/com/csv/isrs.csv");
if (istream == null) {
System.out.println("Cannot access data set, make sure the resources are available.");
System.exit(1);
}
// 如果输入流不为空,初始化reader对象
reader = new CSVReader(new InputStreamReader(istream));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(Arrays.toString(nextLine));
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 释放资源
// 如果reader不为空,则关闭
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
【以下回答由 GPT 生成】
代码中使用的getResourceAsStream方法是从类路径中查找资源文件的方法。该方法会根据给定的资源路径在类路径中查找对应的资源文件,并返回一个输入流。如果找不到相应的资源文件,该方法会返回null。
有以下几种可能导致getResourceAsStream方法返回null的情况:
资源文件的路径不正确:请确保"/isrs.csv"是相对于当前类所在的包的路径,如果不是,请根据实际情况调整路径。
资源文件没有正确地被打包到类路径中:请确保资源文件isrs.csv已经被正确地打包到类路径中。可以通过查看项目的构建配置和检查编译后的输出目录来确认。
资源文件不存在:请确保资源文件isrs.csv存在,并且位于正确的路径下。
类路径配置错误:请确保项目的类路径配置正确,以确保资源文件能够被正确地加载。
下面是一些可以解决这个问题的方法:
final InputStream istream = this.getClass().getResourceAsStream("/path/to/isrs.csv");
确保资源文件被正确地打包到类路径中。
检查资源文件是否存在,可以尝试使用绝对路径来加载资源文件:
final File file = new File("/path/to/isrs.csv");
final InputStream istream = new FileInputStream(file);
如果以上方法都不能解决问题,可能需要进一步检查代码和项目的配置,或者考虑使用其他的文件读取方法来加载资源文件。
总结来说,getResourceAsStream方法返回null的原因可能是资源文件的路径不正确、资源文件没有被正确打包到类路径中、资源文件不存在或者类路径配置错误。可以通过调整资源文件路径、确认资源文件正确打包、检查资源文件是否存在、检查类路径配置来解决这个问题。如果以上方法都不能解决问题,可能需要进一步检查代码和项目的配置,或者使用其他文件读取方法来加载资源文件。