public class ReadData {
public static String file = "d:/test.txt";; public static String readValue(String key) {
Properties props = new Properties();
try {
InputStream in = ReadData.class.getResourceAsStream(file);
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
或者是
public class ReadData {
// public static String file;; public static String readValue(String key,String file) {
Properties props = new Properties();
try {
InputStream in = ReadData.class.getResourceAsStream(file);
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

解决方案 »

  1.   

    那这样吧
    import java.io.InputStream;
    import java.util.Properties;
    public class ReadData {
    private String file;

    public static ReadData getInstance(String file){
    ReadData rd = new ReadData();
    rd.setFile(file);
    return rd;
    }
     
        public String readValue(String key) {
            Properties props = new Properties();
            try {
                InputStream in = ReadData.class.getResourceAsStream(getFile());
                props.load(in);
                String value = props.getProperty(key);
                return value;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } public String getFile() {
    return file;
    } public void setFile(String file) {
    this.file = file;
    }

    public static void main(String[] args) {
    ReadData rd = ReadData.getInstance("d:/test.txt");
    rd.readValue("key");
    }
    }