写入文件属性的路径如何编写?

huangapple go评论91阅读模式
英文:

How write path to file properties?

问题

I wrote a program with two languages. I created a file 'resources_in.properties'. When I attempted to get properties from the file, I got the error:

> Exception in thread "main" java.lang.NullPointerException at
> java.util.Properties$LineReader.readLine(Properties.java:434) at
> java.util.Properties.load0(Properties.java:353) at
> java.util.Properties.load(Properties.java:341) at
> com.rd.java.basic.practice.Helper.getProper(Part5.java:18) at
> com.rd.java.basic.practice.Helper.main(Part5.java:27)

I think it is because I have an incorrect path to properties.
'resources_in.properties' is located at 'MyApp\src\main\resources_in.properties'.
The main class is located at 'MyApp\src\main\java\com\rd\java\basic\practice\Helper.java'

My code:

public class Helper {

    private static String getProper(String lang, String value) {
        Properties prop = new Properties();
        InputStream inputStream = Helper.class.getClassLoader().getResourceAsStream("./src/main/resources_in.properties");
        try {
            prop.load(inputStream);
        }catch (IOException e){
            System.out.println(e);
        }
        String word = prop.getProperty(value);
        return word;
    }
    public static void main(String[] args){
        System.out.println(getProper("en","car"));
    }
}
英文:

I wrote a program with two language. I created a file resources_in.properties. When I attempted to get properties from the file, I got the error:

> Exception in thread "main" java.lang.NullPointerException at
> java.util.Properties$LineReader.readLine(Properties.java:434) at
> java.util.Properties.load0(Properties.java:353) at
> java.util.Properties.load(Properties.java:341) at
> com.rd.java.basic.practice.Helper.getProper(Part5.java:18) at
> com.rd.java.basic.practice.Helper.main(Part5.java:27)

I think it is because I have an incorrect path to properties.
resources_in.properties is located at MyApp\src\main\resources_in.properties.
The main class locate MyApp\src\main\java\com\rd\java\basic\practice\Helper.java

My code:

public class Helper {

    private static String getProper(String lang, String value) {
        Properties prop = new Properties();
        InputStream inputStream = Helper.class.getClassLoader().getResourceAsStream("./src/main/resources_en.properties");
        try {
            prop.load(inputStream);
        }catch (IOException e){
            System.out.println(e);
        }
        String word = prop.getProperty(value);
        return word;
    }
    public static void main(String[] args){
    System.out.println(getProper("en","car"));
    }
}

答案1

得分: 0

你正在尝试加载./src/main/resources_en.properties路径作为资源。这不会起作用,资源是作为类路径路径而不是文件系统文件引用的。通常会是:

Properties prop = new Properties();
try (InputStream in = Helper.class.getResourceAsStream("/resources_en.properties")) {
    prop.load(in);
}

然而,在大多数构建系统中,资源通常放置在/src/main/resource目录下,而在你的情况下,路径是非标准的,这可能会导致JAR文件打包时出现问题。

另外,看起来你正在处理具有文件名中的区域设置后缀的本地化资源。也许你应该使用 java.util.ResourceBundle 而不是 java.util.Properties

英文:

You are trying to load ./src/main/resources_en.properties path as a resource. This won't work, resources are referenced not as file system files but as classpath paths. Usually it would be:

Properties prop = new Properties();
try (InputStream in =
          Helper.class.getResourceAsStream("/resources_en.properties")) {
    prop.load(in);
}

however in most build systems resources are placed under /src/main/resource while in your case the path is non-standard which can imply problems with packaging the JAR.

It also looks like you are dealing with localized resources due to locale suffix in the file name. Perhaps instead of java.util.Properties you should use java.util.ResourceBundle.

huangapple
  • 本文由 发表于 2020年7月23日 05:08:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63043231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定