Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java eclipse资源?_Java_Eclipse_Resources - Fatal编程技术网

Java eclipse资源?

Java eclipse资源?,java,eclipse,resources,Java,Eclipse,Resources,我需要在JavaEclipse中使用资源的som帮助。目前我正在处理塔防,但在导出runnable jar时无法加载它。由于创建地图的级别文件不能作为getFile加载,因此如何更改此代码 public Level getLevel(String fileName){ try{ file = new FileInputStream("level/" + fileName + ".level"); reader = new Input

我需要在JavaEclipse中使用资源的som帮助。目前我正在处理塔防,但在导出runnable jar时无法加载它。由于创建地图的级别文件不能作为getFile加载,因此如何更改此代码

public Level getLevel(String fileName){
        try{
            file = new FileInputStream("level/" + fileName + ".level");
            reader = new InputStreamReader(file);

            scanner = new Scanner(reader);

            level.map = new int[22][18];

我是devolping新手,我从来没有真正了解过资源,现在我找不到一个好的方法来更改它,使它可以与资源URL一起工作,比如:)

既然Jar在类路径上,你应该尝试getResource

URL url = getClass().getResource("level/" + fileName + ".level"); 
InputStream inputStrm = url.openStream(); 

使用以下代码

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("level/" + fileName + ".level").getFile());

描述了比此捕获返回水平更多的内容

;}catch(FileNotFoundException e){e.printStackTrace();}是非常无用的吗?@Sobs catch中的代码取决于我们在
try
中的操作。相应地改变。