package test612;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
public class Tool {
public static String read(String key){
String dd = null;
Properties prop = new Properties();
InputStream fis = null;
String rootPath = System.getProperty("user.dir").replace("\", "/");
try {
File file = new File(rootPath+"/house.properties");
if (!file.exists())
file.createNewFile();
fis = new FileInputStream(rootPath+"/house.properties");
prop.load(fis);
fis.close();//一定要在修改值之前关闭fis
dd=prop.getProperty(key);
} catch (IOException e) {
}
finally{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return dd;
}
public static void write(String key,String value){
Properties prop = new Properties();
String rootPath = System.getProperty("user.dir").replace("\\", "/");
OutputStream os = null;
try {
prop.setProperty(key, value);
try {
os = new FileOutputStream(rootPath+"/house.properties",true);
prop.store(os, "comments for main2");
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
finally{
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
}
}