博客
关于我
Day59.转换流InputStream、OutputStream的使用与字符集 -Java常用类、集合、IO#
阅读量:339 次
发布时间:2019-03-04

本文共 3654 字,大约阅读时间需要 12 分钟。

????InputStream?OutputStream????????????

1. ??????

????????????????????????????????????????????????????????????????????

2. InputStreamReader???

InputStreamReader?Java?????????????????InputStream??????????Reader??????????????FileInputStream????InputStreamReader????????????

?????

import java.io.FileInputStream;import java.io.InputStreamReader;public class InputStreamReaderTest {    public static void main(String[] args) throws Exception {        // ??UTF-8???????        FileInputStream fis = new FileInputStream("message.txt");        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");        char[] buffer = new char[1024];        int len;        while ((len = isr.read(buffer)) != -1) {            String str = new String(buffer, 0, len);            System.out.println(str);        }        isr.close();        fis.close();    }}

3. OutputStreamWriter???

OutputStreamWriter????????????Writer??????????OutputStream?????????????????????????

?????

import java.io.FileOutputStream;import java.io OutputStreamWriter;public class OutputStreamWriterTest {    public static void main(String[] args) throws Exception {        // ?GBK??????????        FileOutputStream fos = new FileOutputStream("message_gbk.txt");        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");        String content = "??GBK??";        osw.write(content.getBytes());        osw.close();        fos.close();    }}

4. ?????

???????????????????????????????????????????????????

  • ASCII????????????????8??????256???????
  • ISO-8859-1?????????????8????256????
  • GB2312?????????????????????????
  • GBK?GB2312???????????????????????
  • Unicode????????????????????
  • UTF-8???????????????????????????

5. ??????????

  • ??????????????????
  • ??????????????????????????????????????
  • ????????????????????????

6. ??????

???????????????????????????????????????

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io InputStreamReader;import java.io OutputStreamWriter;public class FileConvertTest {    public static void main(String[] args) throws Exception {        // ??UTF-8?????        FileInputStream fis = new FileInputStream("message.txt");        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");        char[] buffer = new char[1024];        int len;        while ((len = isr.read(buffer)) != -1) {            System.out.println(new String(buffer, 0, len));        }        isr.close();        fis.close();        // ?????????GBK?????        FileOutputStream fos = new FileOutputStream("message_gbk.txt");        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");        osw.write("message".getBytes());        osw.close();        fos.close();    }}

7. ????

???????IOException?????????????????try-catch-finally?????????????

public class SafeIO {    public static void main(String[] args) throws Exception {        try {            // ????            FileInputStream fis = new FileInputStream("file.txt");            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");            char[] buffer = new char[1024];            while ((len = isr.read(buffer)) != -1) {                System.out.println(new String(buffer, 0, len));            }            isr.close();            fis.close();            // ????            FileOutputStream fos = new FileOutputStream("new_file.txt");            OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");            osw.write("?????".getBytes());            osw.close();            fos.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

8. ??

????Java I/O?????????????????????????????????????????????????????????????????????????????????????????????????

转载地址:http://vuoq.baihongyu.com/

你可能感兴趣的文章
net包之IPConn
查看>>
Net操作配置文件(Web.config|App.config)通用类
查看>>
Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
查看>>
New Relic——手机应用app开发达人的福利立即就到啦!
查看>>
NFinal学习笔记 02—NFinalBuild
查看>>
NFS
查看>>
NFS Server及Client配置与挂载详解
查看>>
NFS共享文件系统搭建
查看>>
nfs复习
查看>>
NFS安装配置
查看>>
NFS的安装以及windows/linux挂载linux网络文件系统NFS
查看>>
NFS的常用挂载参数
查看>>
NFS网络文件系统
查看>>
nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
查看>>
NFV商用可行新华三vBRAS方案实践验证
查看>>
ng build --aot --prod生成文件报错
查看>>
ng 指令的自定义、使用
查看>>
nghttp3使用指南
查看>>
Nginx
查看>>
nginx + etcd 动态负载均衡实践(三)—— 基于nginx-upsync-module实现
查看>>