转换流,Properties 集合

转换流:

OutputStreamWriter  写入转码

字符流通向字节流的桥梁,可使用指定的字符编码表,将要写入流中的字符编码成字节。

格式:

//明确目的地

    FileOutputStream fos=new FileOutputStream("D:\\text\\c.txt",true);

    //

    OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");

    osw.write("你好");

    osw.close();

 

扫描二维码关注公众号,回复: 5726803 查看本文章

 

InputStreamReader  读取转码

字节流通向字符流的桥梁,它使用指定的字符编码表读取字节并将其解码为字符。

格式:

FileInputStream ris=new FileInputStream("D:\\text\\c.txt");

    //转换流

    InputStreamReader isr=new InputStreamReader(ris,"utf-8");

    int len=0;

    while((len=isr.read())!=-1){

       System.out.println((char)len);

    }

 

继承关系:

OutputStreamWriter:  可转换 utf-8 和  gbk

                  |--FileWriter:           gbk

InputStreamReader:

|--FileReader;

newLine() 写入换行方法

 

 

 

 

Properties 集合

Properties集合,它是唯一一个能与IO流交互的集合

特点:

1、Hashtable的子类,map集合中的方法都可以用。

2、该集合没有泛型。键值都是字符串。

3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备

 

load()

列:

Properties pro=new Properties();

//明确数据源

FileInputStream fis=new FileInputStream("D:\\text\\pro.properties");

//读取文件中键值队到集合中

pro.load(fis);//取

System.out.println(pro);

 

store() 存入

列:

Properties pro=new Properties();

FileOutputStream fos=new FileOutputStream("D:\\text\\pro2.properties");

//准备数据

pro.setProperty("name","lisi");

pro.setProperty("age","123");

//存入

pro.store(fos,"111");

猜你喜欢

转载自www.cnblogs.com/hhthtt/p/10632560.html