JAVA读取和写入properties文件

1.读取

        Properties prop = new Properties();
try {
//这个getResourceAsStream方法就是把文件转为inputStream的方式 prop.load(HtpUtil.class.getResourceAsStream("htp.properties")); } catch (IOException e) { e.printStackTrace(); }
//获取某个属性 String host = prop.getProperty("host");

2.写入

        Properties prop = new Properties();
                                   //这儿是获取文件的绝对路径的。       
FileOutputStream fileOutputStream = new FileOutputStream(CrackHash.class.getResource("crack.properties").getFile()); prop.setProperty("cookie", "123456");
//store方法,第一个参数是输入流,第二个是提示信息。#Sun Jan 07 18:47:58 CST 2018 prop.store(fileOutputStream, new Date().toString()); fileOutputStream.flush(); fileOutputStream.close();

这个写入以后,在eclipse里面不一定能立刻看到,先去文件夹里用nodepad++去看一下,写入了就是写入了,有时候重启了eclipse依然看不到。