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

你可能感兴趣的文章
OpenCV使用霍夫变换检测图像中的形状
查看>>
opencv保存图片路径包含中文乱码解决方案
查看>>
OpenCV保证输入图像为三通道
查看>>
OpenCV入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
opencv图像分割2-GMM
查看>>
opencv图像分割3-分水岭方法
查看>>
opencv图像切割1-KMeans方法
查看>>
OpenCV图像处理篇之阈值操作函数
查看>>
opencv图像特征融合-seamlessClone
查看>>
OpenCV图像的深浅拷贝
查看>>
OpenCV在Google Colboratory中不起作用
查看>>
OpenCV学习(13) 细化算法(1)(转)
查看>>
OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
查看>>
OpenCV学堂 | CV开发者必须懂的9种距离度量方法,内含欧氏距离、切比雪夫距离等(建议收藏)
查看>>
OpenCV学堂 | OpenCV中支持的人脸检测方法整理与汇总
查看>>
OpenCV学堂 | OpenCV案例 | 基于轮廓分析对象提取
查看>>
OpenCV学堂 | YOLOv8与YOLO11自定义数据集迁移学习效果对比
查看>>
OpenCV学堂 | YOLOv8官方团队宣布YOLOv11 发布了
查看>>
OpenCV学堂 | YOLOv8实战 | 荧光显微镜细胞图像检测
查看>>
OpenCV学堂 | 汇总 | 深度学习图像去模糊技术与模型
查看>>