作为jar运行时FileNotFoundException [英] FileNotFoundException while running as a jar

查看:203
本文介绍了作为jar运行时FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileInputStream fstream = new FileInputStream(abc.txt)

在运行时抛出FileNotFoundExceptionn一个罐子。为什么?通常它可以在从main方法运行时找到。

is throwing a FileNotFoundExceptionn while running as a jar. Why ? Normally it is able to find while running from main method.

推荐答案

class MyClass{

    InputStream fstream = this.getClass().getResourceAsStream("abc.txt");

}

应使用此代码。
并且应该在Object references类的位置保存文件(在本例中为abc.txt)。这意味着,this.getClass指的是某个文件夹的位置,即com / myfolder / MyClass.java文件夹。

This code should be used. And the files(in this case abc.txt) should be kept , in the Object references class location. That means , this.getClass refers to the location of some folder i.e, com/myfolder/MyClass.java folder .

所以我们应该将abc.txt保存在com / myfolder这个位置。

So we should keep the abc.txt in com/myfolder this location.

这篇关于作为jar运行时FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆