gpt4 book ai didi

java - 使用属性文件为变量赋值的问题

转载 作者:行者123 更新时间:2023-12-02 07:15:39 25 4
gpt4 key购买 nike

大家好,我遇到了一个问题,我有一个属性文件,它存储所有保存位置,我通过以下方式从此文件中获取数据:

public void loadProp() {
System.out.println("Loading properties");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties"); //points to a properties file, this will load up destinations instead of having to declare them here
try {
configProp.load(in);
System.out.println(configProp.getProperty("destinationPDF"));
System.out.println(configProp.getProperty("destination"));
System.out.println(configProp.getProperty("fileList"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("called get username");
username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
System.out.println(username);


}

然后我这样做将值分配给目的地

public String destination = configProp.getProperty("destination");

但是每当我使用目的地时,我都会得到一个空值,但是如果我使用 configProp.getProperty("destination") 我会得到完整路径,我在这里做错了什么,因为我希望该值像其他一样指向目的地类依赖于它

编辑:

This class is called on by a command button (web app)

@ViewScoped
@ManagedBean(name = "fileUploadController")
public class FileUploadController {

public boolean isUploadComplete() { //to enable the next button once finished
return uploadComplete;
}

public void setUploadComplete(boolean uploadComplete) {
this.uploadComplete = uploadComplete;
}

public boolean isUploadComplete2() {
//to disable the file upload button, this will stop users uploading multiple files and over writing them as only the last file uploaded will be used
return uploadComplete;
}

public void setUploadComplete2(boolean uploadComplete) {
this.uploadComplete = uploadComplete;
}
/*
public void handleFileUpload(FileUploadEvent event) {
System.out.println("called");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
*/
//
//Strings for fileUpload
//oadProp()
//public String fileList = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt"; //
private Properties configProp = new Properties();

@PostConstruct
//System.out.println(destinationPDF);
//System.out.println(destination);
// Get the username from the login page, this is used to create a folder for each user
public void loadProp() {
System.out.println("Loading properties");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties"); //points to a properties file, this will load up destinations instead of having to declare them here
try {
configProp.load(in);
System.out.println(configProp.getProperty("destinationPDF"));
System.out.println(configProp.getProperty("destination"));
System.out.println(configProp.getProperty("fileList"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("called get username");
username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
System.out.println(username);


}
//String destinationPDF = configProp.getProperty("destinationPDF"); Always makes a null no idea why yet
//private String destinationPDF = configProp.getProperty("destinationPDF");
public String destination = configProp.getProperty("destination");
private String username;
//public static String destination = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/uploaded/"; // main location for uploads//TORNADO ONLY //"D:/My Documents/NetBeansProjects/printing~subversion/fileupload/uploaded/"; // USE ON PREDATOR ONLY
public static String NewDestination;
public static String UploadedfileName;
public static String CompletefileName;
//
//Strings for file copy
//
//private String destinationPDF = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/"; //USE ON TORNADO//"D:/My Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/";//USE ON PREDATOR
private String NewdestinationPDF;
public static String PdfLocationViewable;
private boolean uploadComplete;
private boolean uploadComplete2;

//
public void File() {

上面是该类的第一段代码

控制台的输出是:

INFO: buttonToUploadText invoked
INFO: Loading properties
INFO: D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/
INFO: D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/
INFO: D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt
INFO: called get username
INFO: null
INFO: destination is null

最佳答案

您的订单已关闭。这是当容器实例化您的 bean 时会发生的情况:

  1. 将调用构造函数
  2. 所有注入(inject)的字段都将被解析
  3. 将调用 PostConstruct。

目前,您正在加载属性之前设置目标值。解决此问题的一个非常简单的方法是在 @PostConstruct 处理程序中设置 destination 值。

@PostConstruct
public void loadProp() {
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("config.properties");
try {
configProp.load(in);
} catch (IOException e) {
e.printStackTrace();
}

destination = configProp.getProperty("destination");
}

与其他方法相比,此方法的一个优点是每次调用 loadProp 方法时都会正确设置 destination 属性(而不是仅调用一次)。

关于java - 使用属性文件为变量赋值的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14962331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com