import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class readProperties {
/**
* @param args
*/
public static void main(String[] args) {
try {
Properties pro =new Properties();
pro.load(new InputStreamReader(Object.class.getResourceAsStream("/1.properties"), "UTF-8"));
System.out.println("读取项目中文件:"+pro.getProperty("name"));
Properties properties = new Properties();
properties.load(new InputStreamReader(new BufferedInputStream(new FileInputStream("D:/Develop/2.properties")),"UTF8"));
System.out.println("读取本地文件:"+properties.getProperty("name"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
properties文件是以键值对的形式存放的
如果出现中文乱码的问题,那么就得加上字符集、
如果后面你还要对properties文件进行其他操作,比如删除file.delete(),那么就得关闭io,解除对properties文件的调用,不然这个文件会删除失败
FileInputStream fIn = new FileInputStream("D:/Develop/2.properties");
Properties properties = new Properties();
properties.load(new InputStreamReader(new BufferedInputStream(fIn), "utf-8"));
fIn.close();//将流关闭