三种流
说明流的三种分类方式
流向:输入流、输出流
数据单位:字节流、字符流
流的角色:节点流、处理流
写出 4 个 IO 流的抽象基类,4 个文件流,4 个缓冲流
InputStream FileXxx BufferedXxx
OutputStream
Reader
Writer
InputStreamReader: 父类 Reader
异常:XxxException XxxError
RandomAccessFile
字节流与字符流的区别与使用情境
字节流:read(byte[] buffer) / read() 处理非文本文件
字符流:read(char[] cbuf) / read() 处理文本文件
使用缓冲流实现 a.jpg 文件复制为 b.jpg 文件的操作
Buffered bis = new BufferedInputStream(new FileInputStream(new File("a.jpg")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("b.jpg")));
byte[] buffer = new byte[1024];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
bos.close();
bis.close();
// 此时的异常应该使用 try-catch-finally 处理。
转换流是哪两个类,分别的作用是什么?请分别创建两个类的对象。
InputStreamReader:将输入的字节流转换为输入的字符流。 解码
OutputStreamWriter:将输出的字符流抓换为输出的字节流。 编码
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");// 文件用什么编码方式写入的,读出时也要用相应的编码读出
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"),"gbk");// 文件用什么编码方式写入的,读出时也要用相应的编码读出
package com.sq;
import java.io.*;
import org.junit.Test;
/**
* 其他流的使用
*
* 1.标准的输入、输出流
*
* 2.打印流
*
* 3.数据流
*
*/
public class OtherStreamTest {
/**
* 1.标准的输入、输出流
*
* 1.1
*
* System.in: 标准的输入流,默认从键盘输入
*
* System.out: 标准的输出流,默认从控制台输出
*
* 1.2
*
* System 类的 setIn(InputStream is) / setOut(PrintStream) 方式重新指定输入和输出的流。
*
* 1.3 练习: 从键盘输入字符串,要求读取到的整行字符串转换成大写输出,然后继续进行输入操作, 直至当输入"e"或者"exit"时,退出程序。
*
* 方法一:使用 Scanner 实现,调用 next()返回一个字符串
*
* 方法二:使用 System.in 实现。System.in ---> BufferedReader 的 readLine()
*
*/
public static void main(String[] args) {
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true) {
System.out.println("请输入字符串:");
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
System.out.println("程序结束!");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* idea 单元测试不支持从键盘输入(见 main 方法)
*
*/
@Test
public void test1() {}
/**
* 2.打印流:PrintStream 和 PrintWriter
*
* 2.1 提供了一系列重载的 print() 和 pringln()
*
* 2.2 练习:
*/
@Test
public void test2() {
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节'\n' 时都会刷新输出缓冲区)
ps = new PrintStream(fos, true);
if (ps != null) {// 把标准输出流(控制台输出)改成文件
System.setOut(ps);// 重新指定一个打印流
}
for (int i = 0; i <= 255; i++) {// 输出 ASCII 字符
System.out.println((char)i);
if (i % 50 == 0) {// 每行 50 个数据一行
System.out.println();// 换行
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
}
/**
* 3.数据流
*
* 3.1 DataInputStream 和 DataOutputStream
*
* 3.2 作用:用于读取或写出基本数据类型的变量或字符串
*
* 练习:将内存中的字符串、基本数据类型的变量写出到文件中
*
* 注意:处理异常的话,仍然应该使用 try-catch-finally
*
*/
@Test
public void test3() {
try {
// 1.
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
// 2.
dos.writeUTF("刘建辰");
dos.flush();// 刷新操作,将内存中的数据写入文件
dos.writeInt(23);
dos.flush();
dos.writeBoolean(true);
dos.flush();
// 3.
dos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
/**
* 将文件中存储的基本数据类型变量和字符串读取到内从中,保存在变量中。
*
* 注意点:读取不同类的数据的顺序要与当初写入文件时,保存的数据的顺序一致!
*/
@Test
public void test4() {
DataInputStream dis = null;
try {
// 1.
dis = new DataInputStream(new FileInputStream("data.txt"));
// 2.
String name = dis.readUTF();
int age = dis.readInt();
boolean isMale = dis.readBoolean();
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("isMale = " + isMale);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3.
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}