Thursday, 15 May 2014

java - issues assigned a value to a variable using a property file -



java - issues assigned a value to a variable using a property file -

this question has reply here:

java .properties file 3 answers

hi having issue, have properties file, stores save locations, info file :

public void loadprop() { system.out.println("loading properties"); inputstream in = this.getclass().getclassloader().getresourceasstream("config.properties"); //points properties file, load destinations instead of having declare them here seek { configprop.load(in); system.out.println(configprop.getproperty("destinationpdf")); system.out.println(configprop.getproperty("destination")); system.out.println(configprop.getproperty("filelist")); } grab (ioexception e) { e.printstacktrace(); } system.out.println("called username"); username = facescontext.getcurrentinstance().getexternalcontext().getremoteuser(); system.out.println(username); }

i assign value destination

public string destination = configprop.getproperty("destination");

but whenever utilize destination null value, if utilize configprop.getproperty("destination") total path, doing wrong here want value point destination other classes depend on it

edit :

this class called on command button (web app) @viewscoped @managedbean(name = "fileuploadcontroller") public class fileuploadcontroller { public boolean isuploadcomplete() { //to enable next button 1 time finished homecoming uploadcomplete; } public void setuploadcomplete(boolean uploadcomplete) { this.uploadcomplete = uploadcomplete; } public boolean isuploadcomplete2() { //to disable file upload button, stop users uploading multiple files , on writing them lastly file uploaded used homecoming 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() + " uploaded."); facescontext.getcurrentinstance().addmessage(null, msg); } } */ // //strings 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); // username login page, used create folder each user public void loadprop() { system.out.println("loading properties"); inputstream in = this.getclass().getclassloader().getresourceasstream("config.properties"); //points properties file, load destinations instead of having declare them here seek { configprop.load(in); system.out.println(configprop.getproperty("destinationpdf")); system.out.println(configprop.getproperty("destination")); system.out.println(configprop.getproperty("filelist")); } grab (ioexception e) { e.printstacktrace(); } system.out.println("called username"); username = facescontext.getcurrentinstance().getexternalcontext().getremoteuser(); system.out.println(username); } //string destinationpdf = configprop.getproperty("destinationpdf"); makes null no thought 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 uploads//tornado //"d:/my documents/netbeansprojects/printing~subversion/fileupload/uploaded/"; // utilize on predator public static string newdestination; public static string uploadedfilename; public static string completefilename; // //strings file re-create // //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() {

above first bit of code class

the output in console :

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 username info: null info: destination null

your ordering off. happen when bean instantiated container:

constructor called all injected fields resolved postconstruct called.

currently, setting destination value before properties have been loaded up. simple solution problem set destination value in @postconstruct handler.

@postconstruct public void loadprop() { inputstream in = this.getclass().getclassloader() .getresourceasstream("config.properties"); seek { configprop.load(in); } grab (ioexception e) { e.printstacktrace(); } destination = configprop.getproperty("destination"); }

one advantage of method on others destination property correctly set every time loadprop method called (as opposed once).

java

No comments:

Post a Comment