JAVA .properties file

后端 未结 3 2097
日久生厌
日久生厌 2021-01-25 14:27

having a lil issue, i have create a properties file :

config.properties located in ../resource/config.properties

this is the file currently :

des         


        
相关标签:
3条回答
  • 2021-01-25 14:45

    Problem is here:

            // destination = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/";  // main location for uploads (CHANGE THIS WHEN USING PREDATOR)
            File theFile = new File(destination + "/" + username);
            theFile.mkdirs();// will create a sub folder for each user (currently does not work, below hopefully is a solution) (DOES NOW WORK)
            System.out.println("Completed Creation of folder");
            NewDestination = destination + username + "/";
    

    You have commented the destination variable and you are using here:

    NewDestination = destination + username + "/";
    
    0 讨论(0)
  • 2021-01-25 14:48

    I wonder whats the issue...I tested your code and it works fine...are you getting compilation error or runtime error?

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    
    public class Test1 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            new Test1().loadProp();
        }
    
        Properties prop = new Properties();
    
        public void loadProp() {
            try {
                prop.load(new FileInputStream("c:/Test/Computer.txt"));
                System.out.println(prop.getProperty("destinationPDF"));
                System.out.println(prop.getProperty("destination"));
                System.out.println(prop.getProperty("fileList"));
    
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    

    Output:

    D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/ D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/ D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt

    0 讨论(0)
  • 2021-01-25 14:53

    You seem to misunderstand what properties files are. They're just data. They don't contain Java code, and aren't used to declare variables. To get the value associated to the key destinationPDF in the properties file, you need to call

    String destinationPDF = prop.getProperty("destinationPDF");
    

    after having initialized the prop variable and loaded the file using prop.load(new FileInputStream(...)). And then you'll have a variable initialized with the value of the key.

    Side note: please respect the Java naming conventions: variables start with a lower-case letter.

    0 讨论(0)
提交回复
热议问题