Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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/8/design-patterns/2.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 指向属性文件中的文件夹和文件的更好方法?_Java_Design Patterns_Properties - Fatal编程技术网

Java 指向属性文件中的文件夹和文件的更好方法?

Java 指向属性文件中的文件夹和文件的更好方法?,java,design-patterns,properties,Java,Design Patterns,Properties,我想知道是否有更好的方法在属性文件中设置指向路径的点。考虑下面的代码: public class Properties { //MIKE public final static String PATH_TO_FILE_A = "C:\\programmer_MIKE\fileA.txt"; public final static String PATH_TO_FILE_B = "C:\\programmer_MIKE\fileB.txt"; //BIL

我想知道是否有更好的方法在属性文件中设置指向路径的点。考虑下面的代码:

public class Properties
{ 
     //MIKE
     public final static String PATH_TO_FILE_A = "C:\\programmer_MIKE\fileA.txt";
     public final static String PATH_TO_FILE_B = "C:\\programmer_MIKE\fileB.txt";

     //BILL
     //public final static String PATH_TO_FILE_A = "/Users/BILL/Desktop/fileA.txt";
     //public final static String PATH_TO_FILE_B = "/Users/BILL/Desktop/fileB.txt";
}
当任何开发人员需要调用文件时,他只需执行以下操作:

File file = new File(Properties.PATH_TO_FILE_A);
如果BILL注释掉MIKE到文件的路径,这对BILL来说是可行的

问:有更好的设计吗?如果比尔把他的工作包括财产档案都交给他,他会给迈克带来麻烦(不用担心,他以后会喝咖啡拿铁)

  • 这些文件很大(2-4Gb),我们不想把它们放在我们的存储库(svn)中,有时只是有一个临时文件夹来创建PDF,所以我们不想把它们放在“/docs”路径中

谢谢你的指点

这样的东西应该在外部配置和/或通过参数、系统参数或环境变量传入。或者,您可以使用DI/IoC,但当没有附加行为时,配置值就足够了


有一个硬编码的默认值是可以的,但是像这样的东西不属于代码。

像这样的东西应该在外部配置和/或通过参数、系统参数或环境变量传入。或者,您可以使用DI/IoC,但当没有附加行为时,配置值就足够了


有一个硬编码的默认值是可以的,但是像这样的东西不属于代码中。

如果出于任何原因您确实必须有硬编码的路径,那么您可以将它们存储在某种按用户名索引的映射中。比如:

public class Properties {

    private static Map<String, DeveloperPaths> properties = create();

    private static Map<String, DeveloperPaths> create() {

        Map<String, DeveloperPaths> properties = new HashMap<String, DeveloperPaths>();

        properties.put("mike", new DeveloperPaths(
                "C:\\programmer_MIKE\fileA.txt", 
                "C:\\programmer_MIKE\fileB.txt")
                );

        properties.put("bill", new DeveloperPaths(
                "/Users/BILL/Desktop/fileA.txt", 
                "/Users/BILL/Desktop/fileB.txt")
                );

        return properties;
    }

    public static File FileA()
    {
        String user = System.getProperty("user.name");
        return properties.get(user).fileA;
    }

    public static File FileB()
    {
        String user = System.getProperty("user.name");
        return properties.get(user).fileB;
    }

}

 class DeveloperPaths {
    public File fileA;
    public File fileB;

    public DeveloperPaths(String pathA, String pathB) {
        fileA = new File(pathA);
        fileB = new File(pathB);
    }
}

如果出于任何原因,您确实必须具有硬编码的路径,那么您可以将它们存储在某种按用户名索引的映射中。比如:

public class Properties {

    private static Map<String, DeveloperPaths> properties = create();

    private static Map<String, DeveloperPaths> create() {

        Map<String, DeveloperPaths> properties = new HashMap<String, DeveloperPaths>();

        properties.put("mike", new DeveloperPaths(
                "C:\\programmer_MIKE\fileA.txt", 
                "C:\\programmer_MIKE\fileB.txt")
                );

        properties.put("bill", new DeveloperPaths(
                "/Users/BILL/Desktop/fileA.txt", 
                "/Users/BILL/Desktop/fileB.txt")
                );

        return properties;
    }

    public static File FileA()
    {
        String user = System.getProperty("user.name");
        return properties.get(user).fileA;
    }

    public static File FileB()
    {
        String user = System.getProperty("user.name");
        return properties.get(user).fileB;
    }

}

 class DeveloperPaths {
    public File fileA;
    public File fileB;

    public DeveloperPaths(String pathA, String pathB) {
        fileA = new File(pathA);
        fileB = new File(pathB);
    }
}

通常路径是可配置的实体,应该存储在属性文件中

属性文件在java中具有内置支持,它使用Properties对象存储该信息

您可以在启动时读取属性文件或应用程序的init(或类似)方法,并从属性文件中读取项目。这将使您的配置动态化,任何人都可以对其进行更改

您可以创建一个静态方法,并在启动时调用它,如下所示:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class GetProperties {
    public static Properties prop = new Properties();

    public static void init() {

        InputStream inputStream = GetProperties.class.getClassLoader().getResourceAsStream("application.properties");
        try {
            prop.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
}

通常路径是可配置的实体,应该存储在属性文件中

属性文件在java中具有内置支持,它使用Properties对象存储该信息

您可以在启动时读取属性文件或应用程序的init(或类似)方法,并从属性文件中读取项目。这将使您的配置动态化,任何人都可以对其进行更改

您可以创建一个静态方法,并在启动时调用它,如下所示:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class GetProperties {
    public static Properties prop = new Properties();

    public static void init() {

        InputStream inputStream = GetProperties.class.getClassLoader().getResourceAsStream("application.properties");
        try {
            prop.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
}

是的,我喜欢。我会用的。谢谢你的提示!是的,我喜欢。我会用的。谢谢你的提示!谢谢你的意见。事实上,我实现了@R Campbell提出的解决方案,它对我来说是可行的。我以后可能会用到你的(因为它是以文件的形式出现的)谢谢你的输入。事实上,我实现了@R Campbell提出的解决方案,它对我来说是可行的。我可能会在将来使用你的(因为它来自一个文件)谢谢