博客
关于我
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/

你可能感兴趣的文章
node中的get请求和post请求的不同操作【node学习第五篇】
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node中自启动工具supervisor的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
Node响应中文时解决乱码问题
查看>>
node基础(二)_模块以及处理乱码问题
查看>>
node安装卸载linux,Linux运维知识之linux 卸载安装node npm
查看>>
node安装及配置之windows版
查看>>
Node实现小爬虫
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node提示:npm does not support Node.js v12.16.3
查看>>
Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
查看>>
Node服务在断开SSH后停止运行解决方案(创建守护进程)
查看>>
node模块化
查看>>
node模块的本质
查看>>
node环境下使用import引入外部文件出错
查看>>
node环境:Error listen EADDRINUSE :::3000
查看>>
Node的Web应用框架Express的简介与搭建HelloWorld
查看>>