转换时可以指定编码格式:GBK、UTF-8

public class Demo {public static void main(String[] args) {File f = new File("word.txt");FileOutputStream out = null;//字节流OutputStreamWriter osw = null;//字节流转字符流BufferedWriter bw = null;//缓冲字符流try {out = new FileOutputStream(f);osw = new OutputStreamWriter(out, "GBK");//字节流转字符流,编码格式GBKbw = new BufferedWriter(osw);bw.write("夕西行");} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//注意关闭顺序,由后至前if (bw != null) {try {bw.close();} catch (IOException e) {e.printStackTrace();}}if (osw != null) {try {osw.close();} catch (IOException e) {e.printStackTrace();}}if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}FileInputStream in = null;//字节流InputStreamReader isr = null;//字节流转字符流BufferedReader br = null;//缓冲字符流try {in = new FileInputStream(f);isr = new InputStreamReader(in, "GBK");br = new BufferedReader(isr);System.out.println(br.readLine());} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}if (isr != null) {try {isr.close();} catch (IOException e) {e.printStackTrace();}}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}
}

关闭流的另外一种方法(推荐)。在try的()中写入代码,try-catch结束,流自动关闭

public class Demo {public static void main(String[] args) {File f = new File("word.txt");//在try的()中写入代码,try-catch结束,流自动关闭try (FileOutputStream out = new FileOutputStream(f);OutputStreamWriter osw = new OutputStreamWriter(out, "GBK");BufferedWriter bw = new BufferedWriter(osw);) {bw.write("夕西行");} catch (IOException e) {e.printStackTrace();}FileInputStream in = null;//字节流InputStreamReader isr = null;//字节流转字符流BufferedReader br = null;//缓冲字符流try {in = new FileInputStream(f);isr = new InputStreamReader(in, "GBK");br = new BufferedReader(isr);System.out.println(br.readLine());} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}if (isr != null) {try {isr.close();} catch (IOException e) {e.printStackTrace();}}if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}
}

转载于:https://www.cnblogs.com/xixixing/p/9526840.html

字节流转字符流OutputStreamWriter、InputStreamReader,关闭流的方法相关推荐

  1. java字节流转字符流的步骤_字节流-java入门基础笔记-51CTO博客

    [14]字节流 一.字节流 1.什么是字节流是IO流中的一种, 可以用来读写字节数据. 2.字节流和字符流的区别计算机中存储任何数据都是以字节的形式, 所以字节流可以读写任意类型的数据. 在读写的数据 ...

  2. 新手小白学java 编码转换流 OutputStreamWriter InputStreamReader

    编码转换流 字节流:针对二进制文件 字符流:针对文本文件,读写容易出现乱码的现象,在读写时,最好指定编码集为UTF-8 1 概述 编码转换流(InputStreamReader/OutputStrea ...

  3. 系统学习JAVA第十七天(字节流、字符流、缓冲的字节流、缓冲的字符流、将字节流转换为缓冲的字符流、面向对象——>字节流转成对象)

    系统学习JAVA第十七天 第一阶段在2021.2.1结束了! 一.数据传输 IO输入和输出,硬盘之间的数据交换 1.文件读写流程 ①创建文件的容器 ②判断方向 合适的类 创建和文件之间的通道 ③ 调用 ...

  4. 1.9 Java转换流:InputStreamReader和OutputStreamWriter

    正常情况下,字节流可以对所有的数据进行操作,但是有些时候在处理一些文本时我们要用到字符流,比如,查看文本的中文时就是需要采用字符流更为方便.所以 Java IO 流中提供了两种用于将字节流转换为字符流 ...

  5. 黑马程序员——Java字符流、字节流IO流

    黑马程序员--Java字符流.字节流Io流 ---------------------- <a href="http://www.itheima.com"target=&qu ...

  6. java 输出流转输入流_输入输出--Java IO流

    O流01 javaIO流概述 ①Java IO即Java 输入输出系统 根据针对数据类型的处理,分为字节流和字符流: 字节流中包含输入流[InputStream]和输出流[OutputStream]两 ...

  7. 学Java基础19 字符流 编码表 转换流 字符高效流 对象流 序列化 以及所有的IO流总结

    一.字符流概述 1.字符流 就是用于来读写字符操作的流 2.字符流使用的原因: 使用字节流来读取文件中中文会出现乱码 因为中文不是一个字节组成的 所以读取中文的时候 需要使用字符流 字符实际上也是使用 ...

  8. zipfile中怎么关闭流_深入学习JAVA -IO流详解

    (一)IO流基本概念 Java对数据的操作是通过流的方式,io是java中实现输入输出的基础,它可以很方便的完成数据的输入输出操作,Java把不同的输入输出抽象为流,通过流的方式允许Java程序使用相 ...

  9. java文件与流_Java文件和流深入

    1.什么是数据流? 数据流是指所有的数据通信通道.有两类流,InputStream and OutputStream,Java中每一种流的基本功能依赖于它们.InputStream用于read,Out ...

最新文章

  1. 腾讯视频会议使用测试
  2. 基类成员的public访问权限在派生类中变为_C++ 派生类的构造函数(学习笔记:第7章 06)...
  3. TP框架笔记 -- 394-model的创建
  4. kafka数据 落盘_Kafka数据可靠性保证三板斧-ACK/ISR/HW
  5. Nginx负载均衡配置和健康检查
  6. c语言链表递增,[C语言][PTA][2019Fall] 6-28 递增的整数序列链表的插入 (15 point(s))
  7. 做开发很久了 Remoting 一直没有碰过,正好最近的项目上面用,就拿出来给大家看看
  8. 踩着七彩祥云来接你的人不一定是意中人,也可能是阿里云
  9. hp打印机驱动android,惠普打印机驱动
  10. C语言实现strcmp函数
  11. 赛门铁克symantec 卸载需要密码解决方法
  12. 常用网页正文提取方法总结
  13. 极客星球 | 图像技术在上亿规模实拍图片中的应用
  14. cesium实现运动模型的相机上帝视角跟随
  15. 3个老鼠确定8个瓶子哪瓶有毒
  16. python perl lisp_巴别塔-编程语言之旅【转】——C、C++、Lisp、Java、Perl、Ruby、Python核心比较...
  17. ERP系统的开发------(整理)
  18. 题解 P4874 【[USACO14DEC] Piggyback_Silver 背负式运输(银)】
  19. Oracle 将多列合并成一列
  20. lisp修改上一个图素_修改图块基点(已解决) - AutoLISP/Visual LISP 编程技术 - CAD论坛 - 明经CAD社区 - Powered by Discuz!...

热门文章

  1. onsize里获取的对话框大小有时会包含滚动条_Python实战分析:获取数据
  2. 码匠编程:CSS让元素绝对居中,你知道几种方法?
  3. web前端是不是没有前景了?
  4. 【C语言】指针进阶 - 指针数组 数组指针 数组指针传参 函数指针 指向函数指针数组的指针
  5. 虚拟网站禁用php,虚拟主机php程序fsockopen函数被禁用
  6. weui开发文档_58小程序云 | 一站式跨平台小程序开发解决方案
  7. android 蓝牙与单片机通信原理图,手机蓝牙与HC-06蓝牙模块控制单片机程序加APP...
  8. 根据用户查进程_Linux进程之如何查看进程详情?
  9. 【Machine Learning 二】单变量线性回归,代价函数,梯度下降
  10. linux虚拟文件系统(五)-文件打开操作分析