Exception in thread "main"不知名的错误!!!!

am139677891 2009-04-15 09:01:11
package aa;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Util {

public Util() {
}

public static byte[] readFile(String filename) throws IOException {

File file = new File(filename);
if (filename == null || filename.equals("")) {
throw new NullPointerException("无效的文件路径");
}
long len = file.length();
byte[] bytes = new byte[(int) len];

BufferedInputStream bufferedInputStream = new BufferedInputStream(new
FileInputStream(file));
int r = bufferedInputStream.read(bytes);
if (r != len) {
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
}

public byte[] readFileJar(String filename) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(
getClass().getResource(filename).openStream());
System.out.println(filename);
int len = bufferedInputStream.available();
byte[] bytes = new byte[len];
int r = bufferedInputStream.read(bytes);
if (len != r) {
bytes = null;
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
}


public static void main(String[] args) {
Util util = new Util();
try {
util.readFile("D:\\XiaoFei\\aaaaaa.txt");
util.readFileJar("D:\\XiaoFei\\aaaaaa.txt");

} catch (IOException ex) {
}
}
}
这是代码!
以下是错误提示:
java.lang.NullPointerException

at aa.Util.readFileJar(Util.java:35)

at aa.Util.main(Util.java:54)

Exception in thread "main"

报错位置:
BufferedInputStream bufferedInputStream = new BufferedInputStream(
getClass().getResource(filename).openStream());
util.readFileJar("D:\\XiaoFei\\aaaaaa.txt");
不知道是什么错误!!!!求高手解决~
...全文
87 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang21cnboy 2009-05-02
  • 打赏
  • 举报
回复
getClass().getResource(filename)

getResouce不能用绝对路径,你的文件放在class 的相对路径上,至于你的相对路径在哪里,要看你启动的时候的路径。
am139677891 2009-04-15
  • 打赏
  • 举报
回复
莪知道哪里报错.!担不知道为什么错~~~报错的位置都写上去了.~~~~~
临远 2009-04-15
  • 打赏
  • 举报
回复
Util.java:35行发生NullPointerException

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧