当前位置: 代码迷 >> 综合 >> java 07file Properties io流
  详细解决方案

java 07file Properties io流

热度:111   发布时间:2023-10-10 21:52:35.0

和IO流结合的方法

方法名 说明
void load(InputStream inStream) 从输入字节流读取属性列表(键和元素对)
void load(Reader reader) 从输入字符流读取属性列表(键和元素对)
void store(OutputStream out, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流
void store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流

 

package oripertiesdemo;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;public class demo3 {public static void main(String[] args) throws IOException {myStore();// 把文件数据加载到集合myLoad();}private static void myLoad() throws IOException {Properties pro = new Properties();FileReader fr = new FileReader("stu\\src\\oripertiesdemo\\fw.txt");pro.load(fr);fr.close();System.out.println(pro);}private static void myStore() throws IOException {Properties pro = new Properties();pro.setProperty("it01","老弟");pro.setProperty("it02","老哥");pro.setProperty("it03","laowu");FileWriter fw = new FileWriter("stu\\src\\oripertiesdemo\\fw.txt");// 集合保存到文件夹中去pro.store(fw,null);fw.close();}
}
public class PropertiesDemo03 {public static void main(String[] args) throws IOException {//把集合中的数据保存到文件
//        myStore();//把文件中的数据加载到集合myLoad();}private static void myLoad() throws IOException {Properties prop = new Properties();//void load(Reader reader):FileReader fr = new FileReader("myOtherStream\\fw.txt");prop.load(fr);fr.close();System.out.println(prop);}private static void myStore() throws IOException {Properties prop = new Properties();prop.setProperty("itheima001","林青霞");prop.setProperty("itheima002","张曼玉");prop.setProperty("itheima003","王祖贤");//void store(Writer writer, String comments):FileWriter fw = new FileWriter("myOtherStream\\fw.txt");prop.store(fw,null);fw.close();}
}