小弟写了一个小项目,其中涉及到使用一个外部文件,代码如下
String basePath = FirstClient.class.getResource("").getPath();
System.out.println(basePath);
InputStream in = null;
try {
in = new FileInputStream(new File(basePath + "测试员5.pfx"));
byte[] buffer = new byte[1000];
in.read(buffer);
System.out.println(buffer);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在IDE中启动能正常运行
1)使用myeclipse中export——>runnable jar file导出jar包,操作如下:

导出后运行jar包时错误如下:

路径有问题,后来小弟又使用fatjar打包,运行结果如下

路径同样有问题,拜托各位高手指点,本人怎么样能正常读取到文件(该文件经打包后与该class文件位于同一目录)
解决方案
30
将这个文件放到jar里,访问方法参考:http://blog.csdn.net/withiter/article/details/11924095
10
你可以把文件路径作为参数传进去,假如文件是固定的,就参考上面的吧。