当前位置:网站首页>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("执行完毕");
}
}
边栏推荐
猜你喜欢
随机推荐
【分布式事务】
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
本体开发日记03-理解代码
GBase数据库中,源为 oracle 报出“ORA-01000:超出打开游标最大数”
Environment build onnxruntime 】
游戏测试的概念是什么?测试方法和流程有哪些?
运行flutter项目时遇到的问题修改flutter为国内镜像
学习栈的心得和总结(数组实现)
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
[Pytorch] Install mish_cuda
These 12 GIS software are better than the other
Summary of steps and methods for installing and uninstalling test cases that you must read
国产谷歌地球同款软件,查看下载19级高清卫星影像so easy!
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
真·鸡汤文
MySQL查漏补缺(五)不熟悉的知识点
Onnx - environment build 】 【 tensorrt
[Environmental Construction] tensorrt
教你如何免费获取0.1米高精度卫星地图
软件测试的流程规范有哪些?具体要怎么做?