共计 4209 个字符,预计需要花费 11 分钟才能阅读完成。
1、转换流概述
OutputStreamWriter 字符输出流
public OutputStreamWriter(OutputStream out)
public OutputStreamWriter(OutputStream out,String charsetName)
InputStreamReader 字符输入流
public InputStreamReader(InputStream in)
public InputStreamReader(InputStream in,String charsetName)
转换流读数据
public static void main(String[] args) throws IOException {// 创建对象 | |
// InputStreamReader isr = | |
//new InputStreamReader(new FileInputStream("osw.txt")); | |
// InputStreamReader isr = | |
//new InputStreamReader(new FileInputStream("osw.txt"), "GBK"); | |
InputStreamReader isr = | |
new InputStreamReader(new FileInputStream("osw.txt"), "UTF-8"); | |
// 读取数据 | |
// 一次读取一个字符 | |
int ch = 0; | |
while ((ch = isr.read()) != -1) {System.out.print((char) ch); | |
} | |
// 释放资源 | |
isr.close();} |
转换流写数据
public static void main(String[] args) throws IOException {// 创建对象 | |
//OutputStreamWriter osw = | |
//new OutputStreamWriter( | |
//new FileOutputStream("osw.txt")); // 默认 GBK | |
//OutputStreamWriter osw = | |
//new OutputStreamWriter( | |
//new FileOutputStream("osw.txt"), "GBK"); // 指定 GBK | |
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"), "UTF-8"); // 指定 UTF-8 | |
// 写数据 | |
osw.write("中国"); | |
// 释放资源 | |
osw.close();} |
2、OutputStreamWriter 写数据
OutputStreamWriter 写数据方法
public void write(int c)
public void write(char[] cbuf)
public void write(char[] cbuf,int off,int len)
public void write(String str)
public void write(String str,int off,int len)
字符流操作要注意的问题
flush() 的作用
flush() 和 close() 的区别 public static void main(String[] args) throws IOException {
// 创建对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
“osw2.txt”));
public static void main(String[] args) throws IOException {// 创建对象 | |
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw2.txt")); | |
// 写数据 | |
// public void write(int c): 写一个字符 | |
// osw.write('a'); | |
// osw.write(97); | |
// 为什么数据没有进去呢? | |
// 原因是:字符 = 2 字节 | |
// 文件中数据存储的基本单位是字节。 | |
// void flush() | |
// public void write(char[] cbuf): 写一个字符数组 | |
// char[] chs = {'a','b','c','d','e'}; | |
// osw.write(chs); | |
// public void write(char[] cbuf,int off,int len): 写一个字符数组的一部分 | |
// osw.write(chs,1,3); | |
// public void write(String str): 写一个字符串 | |
// osw.write("我爱林青霞"); | |
// public void write(String str,int off,int len): 写一个字符串的一部分 | |
osw.write("我爱林青霞", 2, 3); | |
// 刷新缓冲区 | |
osw.flush(); | |
// osw.write("我爱林青霞", 2, 3); | |
// 释放资源 | |
osw.close(); | |
// java.io.IOException: Stream closed | |
// osw.write("我爱林青霞", 2, 3); | |
} |
3、InputStreamReader 读数据
InputStreamReader 读数据方法
public int read()
public int read(char[] cbuf)
public static void main(String[] args) throws IOException {// 创建对象 | |
InputStreamReader isr = | |
new InputStreamReader(new FileInputStream("StringDemo.java")); | |
// 一次读取一个字符 | |
// int ch = 0; | |
// while ((ch = isr.read()) != -1) { | |
// System.out.print((char) ch); | |
// } | |
// 一次读取一个字符数组 | |
char[] chs = new char[1024]; | |
int len = 0; | |
while ((len = isr.read(chs)) != -1) {System.out.print(new String(chs, 0, len)); | |
} | |
// 释放资源 | |
isr.close();} |
4、字符流复制文本文件
把当前项目目录下的 a.txt 内容复制到当前项目目录下的 b.txt 中
把 c:\a.txt 内容复制到 d:\b.txt 中
/* | |
* 需求:把当前项目目录下的 a.txt 内容复制到当前项目目录下的 b.txt 中 | |
* | |
* 数据源:* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader | |
* 目的地:* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter | |
*/ | |
public class CopyFileDemo {public static void main(String[] args) throws IOException {// 封装数据源 | |
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt")); | |
// 封装目的地 | |
OutputStreamWriter osw = | |
new OutputStreamWriter(new FileOutputStream("b.txt")); | |
// 读写数据 | |
// 方式 1 | |
// int ch = 0; | |
// while ((ch = isr.read()) != -1) { | |
// osw.write(ch); | |
// } | |
// 方式 2 | |
char[] chs = new char[1024]; | |
int len = 0; | |
while ((len = isr.read(chs)) != -1) {osw.write(chs, 0, len); | |
// osw.flush(); | |
} | |
// 释放资源 | |
osw.close(); | |
isr.close();} | |
} |
5、转换流的简化写法
转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,所以,为了简化我们的书写,转换流提供了对应的子类。
FileWriter
FileReader
OutputStreamWriter = FileOutputStream + 编码表 (GBK)
FileWriter = FileOutputStream + 编码表 (GBK)
InputStreamReader = FileInputStream + 编码表 (GBK)
FileReader = FileInputStream + 编码表 (GBK)
正常的字符流是需要对字节流进行包装的,而便捷字符流不需要我们手动进行字节流的包装,只要给文件路径字符串就可以了。
public static void main(String[] args) throws IOException { | |
// 封装数据源 | |
FileReader fr = new FileReader("a.txt"); | |
// 封装目的地 | |
FileWriter fw = new FileWriter("b.txt"); | |
// 一次一个字符 | |
// int ch = 0; | |
// while ((ch = fr.read()) != -1) {// fw.write(ch); | |
// } | |
// 一次一个字符数组 | |
char[] chs = new char[1024]; | |
int len = 0; | |
while ((len = fr.read(chs)) != -1) {fw.write(chs, 0, len); | |
fw.flush();} | |
// 释放资源 | |
fw.close(); | |
fr.close();} |
6、FileWriter 和 FileReader
FileWriter 写数据
FileReader 读取数据
FileWriter 和 FileReader 实现文本文件的复制
public static void main(String[] args) throws IOException {// 封装数据源 | |
FileReader fr = new FileReader("a.txt"); | |
// 封装目的地 | |
FileWriter fw = new FileWriter("b.txt"); | |
// 一次一个字符 | |
// int ch = 0; | |
// while ((ch = fr.read()) != -1) { | |
// fw.write(ch); | |
// } | |
// 一次一个字符数组 | |
char[] chs = new char[1024]; | |
int len = 0; | |
while ((len = fr.read(chs)) != -1) {fw.write(chs, 0, len); | |
fw.flush();} | |
// 释放资源 | |
fw.close(); | |
fr.close();} |
