Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/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_Eclipse_File_Tomcat_War - Fatal编程技术网

在Java中运行时创建配置文件

在Java中运行时创建配置文件,java,eclipse,file,tomcat,war,Java,Eclipse,File,Tomcat,War,我用eclipse做了一个web项目,在TomcatV7上运行我的web服务。我有一个配置文件,它必须定义一些变量,这些变量应该在Web服务器启动时更改,而不需要重新启动它。问题是:我可以把这个文件放在哪里? 目前,我已经将其放入“/”目录中,但当我导出WAR文件并将其安装到Web服务器上时,它似乎无法工作。有没有办法在WAR中创建此文件并在运行时对其进行修改 下面是访问配置文件的.class文件的部分代码 public class ToolConfigurations { privat

我用eclipse做了一个web项目,在TomcatV7上运行我的web服务。我有一个配置文件,它必须定义一些变量,这些变量应该在Web服务器启动时更改,而不需要重新启动它。问题是:我可以把这个文件放在哪里? 目前,我已经将其放入“/”目录中,但当我导出WAR文件并将其安装到Web服务器上时,它似乎无法工作。有没有办法在WAR中创建此文件并在运行时对其进行修改

下面是访问配置文件的.class文件的部分代码

public class ToolConfigurations {
    private static final Logger log = LogManager.getLogger(ToolConfigurations.class);   //Oggetto logging

    private static ToolConfigurations instance = null;  //verificarne la necessità

    private static String defaultServerName = null;
    private static String defaultDbName = "postgres";
    private static String defaultDbUser = "postgres";
    private static String defaultDbPass = "password";
    private static int defaultDbMaxConnNum = 10;

    private static String configFilePath = ".\\";
    private static String configFileName = "tool.properties";
    private String serverName = null;
    private String dbName = null;
    private String dbUser = null;
    private String d`enter code here`bPass = null;
    private int dbMaxConnNum = -1;

    private ToolConfigurations() throws Exception {
        File file = new File(configFilePath + configFileName);
        if(file.exists()) {
            //file configurazione esiste
            FileReader in = new FileReader(file);
            BufferedReader br = new BufferedReader(in);
            String line = null;

            while((line = br.readLine()) != null) { //Leggo riga per riga il file 
                String[] values = line.split(" ");
                switch (values[0]) {
                    case "serverName":
                        serverName = values[1];
                        break;
                    case "dbName":
                        dbName = values[1];
                        break;
                    case "dbUser":
                        dbUser = values[1];
                        break;
                    case "dbPass":
                        dbPass = values[1];
                        break;
                    case "dbMaxConnNum":
                        dbMaxConnNum = Integer.parseInt(values[1]);
                        break;
                    default:
                        log.warn("Elemento inaspettato nel file di configurazione: " + values[0]);
                        break;
                }
            }

            br.close();
            in.close();
        }else {
            if(file.createNewFile() == false) {
                //Errore creazione file
                log.error("Errore creazione file di configurazione");
                throw new Exception("Errore creazione file configurazione");
            }
            //CREO FILE CONFIGURAZIONE CON IMPOSTAZIONI DI DEFAULT
            FileWriter fw = new FileWriter(file);
            fw.write("dbName " + defaultDbName + "\r\n");
            fw.write("dbUser " + defaultDbUser + "\r\n");
            fw.write("dbPass " + defaultDbPass + "\r\n");
            fw.write("dbMaxConnNum " + defaultDbMaxConnNum + "\r\n");
            fw.flush();
            fw.close();
            log.warn("File di configurazione non trovato. Path: " + file.getAbsolutePath() + ". Creato nuovo file con configurazioni di default.");
            //CARICO IMPOSTAZIONI DI DEFAULT
            dbName = defaultDbName;
            dbUser = defaultDbUser;
            dbPass = defaultDbPass;
            dbMaxConnNum = defaultDbMaxConnNum;
        }
    }

    public static synchronized ToolConfigurations getInstance() throws Exception {
        if(instance == null) {
            instance = new ToolConfigurations();
        }
        return instance;
    }
    ...

在web服务器中,除.class以外的文件由服务器直接负责。您不需要重新加载服务器


但是尝试使用.properties文件,并在服务器上部署时检查WAR的结构。

我找到的最佳解决方案是使用类加载器搜索我以前在主目录中创建的配置文件。 使用示例如下:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream fileInput = classLoader.getResourceAsStream(configFileName);
if(fileInput == null){
    //File not found
}else{
    //File found
    InputStreamReader sr = new InputStreamReader(fileInput);
    BufferedReader br = new BufferedReader(sr);
    try {
        while((line = br.readLine()) != null) { 
            //Read file line by line
        }
    }catch (IOException e) {
        throw new IOException("Error parsing file: " + e.getMessage());
    }
}

是的,我使用的是.properties文件,但问题是:如何相对链接到它?请发布部分代码,特别是如何访问文件。WAR文件不打算在运行时修改。也就是说,你可以操纵一场爆发(解压)的战争。我建议不要这样做。您可能会发现将配置放在JNDI中是一个更好的主意。我需要一个独立于环境放置和访问配置文件的地方。