当前位置:网站首页>5.转换流
5.转换流
2022-08-09 09:23:00 【过来我的小熊】
转换流
- 桥转换流 (InputStreamReader(字节流->字符流)/OutputStreamWriter(字符流->字节流))
- 可将字节流转换为字符流
- 可设置字符的编码方式
InputStreamReader读取文件,指定使用的编码
package com.io.zifu;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 使用InputStreamReader读取文件,指定使用的编码
*/
public class Demo7 {
public static void main(String[] args) throws IOException {
// 创建InputStreamReader对象,字节流->字符流
FileInputStream fr = new FileInputStream("E:\\桌面\\aaa.txt");
InputStreamReader isr = new InputStreamReader(fr,"utf-8");
// 读取文件
int data = 0;
while ((data=isr.read())!=-1){
System.out.print((char) data);
}
// 关闭
isr.close();
}
}
OutputStreamWriter
package com.io.zhl;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* OutputStreamWriter 写入文件,使用指定的编码
*/
public class Demo2 {
public static void main(String[] args) throws IOException {
// 创建OutputStreamWriter对象
FileOutputStream fos = new FileOutputStream("E:\\桌面\\aaa.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
// 写入
for (int i = 0; i < 10; i++) {
osw.write("加油学习Java\n");
}
// 关闭
osw.close();
System.out.println("执行完毕");
}
}
边栏推荐
猜你喜欢
测试用例的原则、缺陷报告怎么写你都知道吗?
Read file by byte and character_load configuration file
学习双向链表的心得与总结
MySQL transaction isolation
The era of Google Maps is over, how to view high-definition satellite image maps?
软件测试面试思路技巧和方法分享,学到就是赚到
自动化测试框架有哪几种?搭建的思路是什么?一篇文章让你彻底了解自动化
WAVE SUMMIT 2022深度学习开发者峰会
本体开发日记05-努力理解SWRL(中)
MySQL indexes
随机推荐
功能自动化测试实施的原则以及方法有哪些?
【个人学习总结】CRC校验原理及实现
软件测试个人求职简历该怎么写,模板在这里
MySQL事务隔离
.equals ==
Redis high availability
本体开发日记01-Jena配置环境变量
Domestic Google earth, terrain analysis seconds kill with the map software
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
MySQL Checking and Filling Leaks (5) Unfamiliar Knowledge Points
这12个GIS软件一个比一个好用
一篇文章让你彻底搞懂关于性能测试常见术语的定义
网络安全入门基础:IP地址
手机APP测试流程规范和方法你知道多少?
列表
Teach you how to get a 0.1-meter high-precision satellite map for free
本体开发日记05-努力理解SWRL(中)
性能测试包括哪些方面?分类及测试方法有哪些?
Swap swap partition
Django实现对数据库数据增删改查(二)