java 如何使用Java在多个平台上正确加载文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10594316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:44:36  来源:igfitidea点击:

How to load files on multiple platforms properly using Java?

javapathfilepath

提问by Harsha

I have a java swing database application which needs to be run on Windows and Linux. My database connection details are stored in a XML file and I load them.

我有一个需要在 Windows 和 Linux 上运行的 java swing 数据库应用程序。我的数据库连接详细信息存储在 XML 文件中并加载它们。

This application can load this properties on Linux properly but it is not working on Windows.

此应用程序可以在 Linux 上正确加载此属性,但在 Windows 上无法正常工作。

How do I load files on multiple platforms properly using Java?

如何使用 Java 在多个平台上正确加载文件?



This is the code:

这是代码:

PropertyHandler propertyWriter = new PropertyHandler();

List keys = new ArrayList();
keys.add("ip");
keys.add("database");
Map localProps = propertyWriter.read(keys, "conf" + File.separatorChar + "properties.xml", true);//if false load from the local properties

//get properties from the xml in the internal package
List seKeys = new ArrayList();
seKeys.add("driver");
seKeys.add("username");
seKeys.add("password");

Map seProps = propertyWriter.read(seKeys, "conf" + File.separatorChar + "properties.xml", true);

String dsn = "jdbc:mysql://" + (String) localProps.get("ip") + ":3306/" + (String) localProps.get("database");
jDBCConnectionPool = new JDBCConnectionPool((String) seProps.get("driver"), dsn, (String) seProps.get("username"), (String) seProps.get("password"));

File reader method:

文件读取方法:

public Map read(List properties, String path, boolean isConfFromClassPath)
{
    Properties prop = new Properties();
    Map props = new HashMap();
    try {

        if (isConfFromClassPath) {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
            prop.loadFromXML(in);

            for (Iterator i = properties.iterator(); i.hasNext();) {
                String key = (String) i.next();
                props.put(key, prop.getProperty(key));
            }
            in.close();

        } else {
            FileInputStream in = new FileInputStream(path);
            prop.loadFromXML(in);

            for (Iterator i = properties.iterator(); i.hasNext();) {
                String key = (String) i.next();
                props.put(key, prop.getProperty(key));
            }
            in.close();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return props;
}

回答by yiannis

If the file is in a jar file and accessed by the classpath then you should always use /.

如果该文件位于 jar 文件中并由类路径访问,则应始终使用/.

The JavaDocs for the ClassLoader.getResourcesay that "The name of a resource is a '/'-separated path name that identifies the resource."

JavaDocsClassLoader.getResource说“资源的名称是一个以 '/' 分隔的路径名,用于标识该资源。”

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

回答by Cephalopod

I'm not sure if there is theproper way, but one way is:

我不知道是否有适当的方式,但一个方法是:

File confDir = new File("conf");
File propFile = new File(confDir, "properties.xml");

But in a scenario as simple as yours, I would just use /

但在像你这样简单的场景中,我只会使用 /

回答by Radadiya Nikunj

You can load all files on multiple platforms without any trouble.

您可以轻松地在多个平台上加载所有文件。

Kindly use Matcher.quoteReplacement(File.separator) for replacing the slash.

请使用 Matcher.quoteReplacement(File.separator) 替换斜线。

It will works for every platform.

它适用于每个平台。

String fileLocation = "/src/service/files";

fileLocation  = fileLocation.replaceAll("/",Matcher.quoteReplacement(File.separator));

回答by James Gan

If it's a resource located in classpath, we can load it with following snippet:

如果它是位于类路径中的资源,我们可以使用以下代码段加载它:

getClass().getClassLoader().getResourceAsStream(
    "/META-INF/SqlQueryFile.sql")));

回答by GreyBeardedGeek

assuming that your file is in conf/properties.xml on Linux and conf\properties.xml on Windows, use File.pathSeparator instead of File.separator

假设您的文件在 Linux 上的 conf/properties.xml 和 Windows 上的 conf\properties.xml 中,请使用 File.pathSeparator 而不是 File.separator