获取文件的绝对路径

时间:2015-06-16 23:37:03

标签: java absolute-path

所以我试图使用BufferedImages因为它们更加便宜而且似乎加载它们有点多了。我认为你需要加载它们的绝对路径,我已经尝试过只做src / Package.image.png但是当我导出到runnable jar时它不起作用。这段代码在eclipse中运行,但出于某种原因,当我将它导出为.jar时它不起作用。

package MainGame;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

 public class GraphicsPath {





public static String getGraphicsPath(){


    String path = null;

    PrintWriter writer = null;
    try {
        writer = new PrintWriter("text.txt");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    path = GraphicsPath.class.getProtectionDomain().getCodeSource().getLocation().getPath();  


    System.out.println(path);

    writer.println("orignal path    " + path);

    //FOR LINUX
    String[] splited = path.split("/");


    //FOR WINDOWS
    //String[] splited = path.split("\");
    writer.println(path);


    path = path.replace("%20", " ");

    path += "MainGame/";

    writer.println(path);
    writer.close();

    System.out.println(path);
    return path;

}

}

以下是text.txt导出到jar

时的内容
orignal path    ./
./
./MainGame/

以下是仍然在java项目中的text.txt的内容

orignal path    /home/mini/workspace/Pokemon%20Game/bin/
/home/mini/workspace/Pokemon%20Game/bin/
/home/mini/workspace/Pokemon Game/bin/MainGame/

3 个答案:

答案 0 :(得分:1)

让它变得有点容易,如果你把你的图像资源放在与BufferedImage image; try (InputStream in = GraphicsPath.class.getResourceAsStream('my-image.png')) { if (in != null) { image = ImageIO.read(in ); } } 相同的目录中(两者都在类路径上),你可以做到

java -jar

这就是你想要做的事情。如果您使用EAR,WAR,JNLP,var geocoder; var map; function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(-34.397, 150.644); var mapOptions = { zoom: 8, center: latlng } map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); geocoder.geocode( { 'address': '5th Avenue New York' }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); } else { alert('Geocode was not successful for the following reason: ' + status); } }); } google.maps.event.addDomListener(window, 'load', initialize); ,或者我的道歉小程序,使用路径可能甚至不起作用。在您知道的类旁边捆绑资源意味着您可以轻松访问这些资源。

现在,在您的情况下,它很笨重,因为它们不会被复制到bin目录中,因此您可能需要编辑用于开发的类路径。

答案 1 :(得分:0)

您可以通过多种方式传递路径。请检查下面的一些,也许其中一个可以为你工作:

Directory structure

.Failed

有关文件路径的更多信息here

答案 2 :(得分:0)

如果我理解正确,你有一个需要阅读的package.image.png文件,它在.jar文件中。

之前我遇到过这个问题,我认为你不能使用路径找到这个文件。在我的一个项目中,我实际使用了一个URL来指定文件的位置。我会举一个例子说明我做了什么

所以在我的jar文件中,有一个images文件夹。说我有/images/x.png

我这样做

URL u = this.getClass().getResource( "/images/x.png" );
a=ImageIcon(u);

我用它来创建imageIcon,但我想你可以查看如何用URL读取文件。

编辑:我刚试过这个来读取文件,它可以工作。

URL test =Class.class.getResource( "/docs/test.txt" );
BufferedReader in = new BufferedReader(new InputStreamReader(test.openStream()));

所以你可以用其他东西包装InputStreamReader来获取图像

相关问题