从与.jar文件位于同一文件夹的文件中读取

问题描述 投票:16回答:8

有没有办法从与.jar文件位于同一文件夹的文件中读取?我正在尝试创建一个java程序,当我上交我的作业时必须从文件中读取。我转过来后,我不知道文件或程序将保存在哪里,所以我不认为我可以对保存文件的目录进行编码。这可能吗?问题是我必须将文件上传到CSX主机服务器,这是一个Linux服务器并从那里运行它。如果我没有在文件之前放置路径,它只搜索其当前文件夹位置?

java file jar
8个回答
17
投票

如注释中所述,仅当您从jar所在的目录运行命令时,此方法才有效。

(在桌面应用程序的上下文中) 要访问jar当前目录中的文件,您的文件path前面应加一个点。例:

String path = "./properties.txt";
FileInputStream fis = new FileInputStream(path);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
// Read the file contents...

在此示例中,有一个名为properties.txt的文本文件与jar位于同一目录中。 该文件将由jar中包含的程序读取。 编辑:你说文件名不会改变,这个答案适用,因为你事先知道名字,当然,如果你喜欢硬编码它。


22
投票

您可以通过以下方式获取包含任何特定类的JAR文件的位置:

URL url = thisClass.getProtectionDomain().getCodeSource().getLocation();

从那里可以很容易地将URL重新激活到所需的文件。


2
投票

如果输入文件名可以硬编码,并且您知道它将始终与包含您的代码的jar位于同一目录中,那么这应该可以工作:

public static void main(String[] args) {

    try {
        // Determine where the input file is; assuming it's in the same directory as the jar
        String fileName = "input.txt";
        File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
        String inputFilePath = jarFile.getParent() + File.separator + fileName;         
        FileInputStream inStream = new FileInputStream(new File(inputFilePath));

        try {

            // Read in the contents of the input file in a single gulp
            FileChannel fc = inStream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
                    fc.size());

            // Do something with the read in data
            System.out.println(Charset.defaultCharset().decode(bb)
                    .toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            inStream.close();
        }

    } catch (IOException | URISyntaxException e) {
        e.printStackTrace();
    }

}

2
投票

@kcpr - 感谢您提供的解决方案。下面一个为我工作。

private static String CONFIG_FILE_LOCATION = "lib"+ File.separator + "Config.properties";

static {
    File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
    String inputFilePath = jarFile.getParent() + File.separator + CONFIG_FILE_LOCATION; 
    propConfig = new PropertiesConfiguration(new File(inputFilePath));
    propConfig.load();
}

1
投票

为了使其“自包含”,将资源放入Jar中。然后它成为一个(只读)embedded resource,我们可以使用以下内容获取URL:

URL url = this.getClass().getResource("/path/to/the.resource");

0
投票

根据您的评论,我认为您最好的解决方案是将上传文件的绝对路径传递到jar中。

您可以将路径作为命令行参数传递给jar。由于路径和文件名不会更改,并且您从linux运行它,因此您可以创建一个.sh脚本来运行它。

#!/bin/bash    
java -jar myjar.jar /path/to/file/filename.txt

这也使您免于文件路径中的硬编码或文件名到jar中。


0
投票

以下是如何获得jar文件的干净目录路径(基于EJP的答案):

URL url = IssueInfoController.class.getProtectionDomain().getCodeSource().getLocation();
String urlString = url.toString();
int firstSlash =  urlString.indexOf("/");
int targetSlash = urlString.lastIndexOf("/", urlString.length() - 2) + 1;

return urlString.substring(firstSlash, targetSlash) + YOUR_FILE;

-1
投票
public class TestClassLoaderAccess {
    public static void main(String[] argv) {
        TestClassLoaderAccess me = new TestClassLoaderAccess();
        ClassLoader myLoader = me.getClass().getClassLoader();
        System.out.println(myLoader.getClass().getSuperclass().getName());
        java.net.URLClassLoader myUrlLoader = (java.net.URLClassLoader) myLoader;
        java.net.URL resource = myUrlLoader.findResource("TestClassLoaderAccess.class");
        System.out.println(resource.toString());
    }
}

运行它:

C:\JavaTools>javac TestClassLoaderAccess.java

C:\JavaTools>java TestClassLoaderAccess
java.net.URLClassLoader
file:/C:/JavaTools/TestClassLoaderAccess.class

C:\JavaTools>

(第一个println只是为了证明类加载器是URLClassLoader的子类。)

© www.soinside.com 2019 - 2024. All rights reserved.