1

有一个 lil 问题,我创建了一个属性文件:

config.properties 位于 ../resource/config.properties

这是当前的文件:

destinationPDF=D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/
destination="D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/
fileList =D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt

我完成属性文件了吗?

我也想访问这个文件并将变量加载到一个类中

我试过了

public void loadProp() {
    try {
        prop.load(new FileInputStream("../resources/config.properties"));
        System.out.println(prop.getProperty("destinationPDF"));
        System.out.println(prop.getProperty("destination"));
        System.out.println(prop.getProperty("fileList"));

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

但是现在该类将无法编译,因为它例如找不到变量目标,那么我如何从文件中加载变量,我还需要清除类中的变量吗?

对不起,如果这些是愚蠢的问题,第一次使用属性!

如果我通常输入变量,我不会收到此错误

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 Properties configProp = new Properties();

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

编辑2:

public void loadProp() {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties");
    try {
        prop.load(in);
        System.out.println(prop.getProperty("destinationPDF"));
        System.out.println(prop.getProperty("destination"));
        System.out.println(prop.getProperty("fileList"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Properties prop = new Properties();
private String destinationPDF = prop.getProperty("destinationPDF");
public String destination = prop.getProperty("destination");

它正在工作,没有错误等,但目的地和目的地PDF正在传递空值

4

3 回答 3

3

您似乎误解了属性文件是什么。它们只是数据。它们不包含 Java 代码,也不用于声明变量。要获取与属性文件中的键关联的值destinationPDF,您需要调用

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

在初始化prop变量并使用prop.load(new FileInputStream(...)). 然后您将使用键的值初始化一个变量。

旁注:请尊重 Java 命名约定:变量以小写字母开头。

于 2013-02-16T16:26:31.980 回答
1

问题在这里:

        // 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 + "/";

您已经注释了目标变量,并且您在此处使用:

NewDestination = destination + username + "/";
于 2013-02-16T16:25:28.010 回答
0

我想知道是什么问题...我测试了您的代码,它工作正常...您遇到编译错误还是运行时错误?

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();
        }
    }

}

输出:

D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/ D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/ D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources /目录文件/directoryFiles.txt

于 2013-02-16T16:18:55.853 回答