13.2 IO流原理及流的分类
- I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于
处理设备之间的数据传输。如读/写文件,网络通讯等。 - Java程序中,对于数据的输入/输出操作以“流(stream)” 的
方式进行。 - Java.io包下提供了各种“流”类和接口,用以获取不同种类的
数据,并通过标准的方法输入或输出数据。
流的分类 - 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
- 按数据流的流向不同分为:输入流,输出流
- 按流的角色的不同分为:节点流,处理流
(抽象基类) 字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer
- Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个
抽象基类派生的。 - 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
13.3 节点流
package auguigu.java;
import org.junit.Test;
import java.io.*;
/**
* 一、流的分类:
* 1.操作数据单位:字节流、字符流
* 2.数据的流向:输入流、输出流
* 3.流的角色:节点流、处理流
*
* 二、流的体系结构
* 抽象基类 节点流(或文件流) 缓冲流(处理流的一种)
* InputStream FileInputStream (byte) BufferInputStream
* OutputStream FileOutputSteam (byte) BufferOutputStream
* Reader FileReader (char) BufferReader
* Writer FileWriter (char) BufferWriter
*
*
* @author xq
* @create 2021-01-03-10:32
*/
public class FileReaderWriterTest {
/*
将Day09下的hello.txt文件内容读入程序中,并输出到控制台
说明点:
1.read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
2.异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
3.读入的文件一定要存在,否则就会报FileNotFoundException.
*/
@Test
public void testFileReader(){
//1.实例化File类的对象,指明要操作的文件
File file = new File("Hello.txt");//相较于当前Module
//2.提供具体的流
FileReader fr = null;
try {
//3.数据的读入
//read():返回读入的一个字符,如果达到文件末尾,返回-1
int data = fr.read();
while (data != -1){
System.out.print((char)data);
data = fr.read();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流的关闭操作
try {
if(fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//对Read()操作升级:使用read的重载方法
@Test
public void testFileReader1() throws IOException {
//1.File类的实例化
File file = new File("Hello.txt");
//2.FileReader流的实例化
FileReader fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
System.out.println(cbuf);
}
//4.资源的关闭
fr.close();
}
/*
从内存中写出数据到硬盘的文件里
说明:
1.输出操作,对应的File可以不存在,并不会报异常
2.
File对应中的硬盘文件,如果不存在,在输出的过程中,会自动创建此文件
如果存在:
如果流使用的构造器是:FileWriter(file,false)/FileWriter(file):对原有文件的覆盖
如果流使用的构造器是:FileWriter(file,true)不对原有文件的覆盖,而是追加操作
*/
@Test
public void testFileWriter() throws IOException {
//1.提供File类的对象,指明写出到的文件
File file = new File("hello1.txt");
//2.提供FileWriter的对象,用于数据的写出
FileWriter fw = new FileWriter(file);
//3.写出的操作
fw.write("I hava a dream!\n".toCharArray());
fw.write("you need to hava a dream!");
//4.流资源的关闭操作
fw.close();
}
@Test
public void testFileReaderFileWriter(){
FileWriter fw = null;
FileReader fr = null;
try {
//1.创建File类的对象,指明读入和写出的文件
File srcFile = new File("Hello.txt");
File destFile = new File("Hello2.txt");
//2.创建输入流和输出流的对象
fw = new FileWriter(destFile);
fr = new FileReader(srcFile);
//3.数据的读入和写出操作
char[] cbuf = new char[5];
int len;//记录每次读入到cbuf数组中的字符的个数
while((len = fr.read(cbuf)) != -1){
//每次写出len个字符
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流资源
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}package auguigu.java;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* 测试FileInputStream和FileOutputStream的使用
*
* 结论:
* 1.对于文本文件(.txt,.java,.c,.cpp),使用字符流来处理
* 2.对于非文本文件(.jpg,.mp3,,.mp4,.avi,.doc.ppt..),使用字节流处理
*
* @author xq
* @create 2021-01-03-14:16
*/
public class FileInputOutputStreamTest {
//使用字节流FileInputStream可能出现乱码
@Test
public void testFileInputStream(){
FileInputStream fis = null;
try {
//1.造文件
File file = new File("Hello.txt");
//2.造流
fis = new FileInputStream(file);
//3.读数据
byte[] buffer = new byte[5];
int len;//记录每次读取字节的个数
while ((len = fis.read(buffer)) != -1){
String str = new String(buffer,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭资源
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
13.4 缓冲流
package auguigu.java;
import org.junit.Test;
import java.io.*;
/**
* 处理流之一:缓冲流的使用
*
* 1.缓冲流:
* BufferInputStream
* BufferOutputStream
* BufferReader
* BufferWriter
*
*
* 2.作用:提高流的读取、写入速度
* 提高读写速度的原因:内部提供了一个缓冲区
*
* 3.处理流,就是“套接”在已有流的基础上。
*
* @author xq
* @create 2021-01-03-14:50
*/
public class BufferTest {
/*
实现非文件的复制
*/
@Test
public void BufferedStreamTest() throws FileNotFoundException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File srcFile = new File("Hello.txt");
File destFile = new File("Hello3.txt");
//2.造流
//2.1造节点流
FileInputStream fis = new FileInputStream((srcFile));
FileOutputStream fos = new FileOutputStream(destFile);
//2.2造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.复制的细节:读取、写入
byte[] buffer = new byte[10];
int len;
while ((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.资源关闭
//要求:先关闭外层的流,再关闭内层的流
try {
if(bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
//说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略
// fis.close();
// fos.close();
}
}
}13.5转换流
package auguigu.java;
import org.junit.Test;
import java.io.*;
/**
* 处理流之二:转换流的使用
* 1.转换流:属于字符流
* InputStreamReader:将一个字节的输入流转换为字符的输入流
* OutputStreamWriter:将一个字符的输出流转换为字节的输出流
*
* 2.作用:提供字节流与字符流之间的转换
*
* 3.解码:字节、字节数组--->字符数组,字符串
* 编码:字符数组、字符串---->字节,字节数组
*
* 4.字符集
* 提供了在字节流和字符流之间的转换
* ASCII:美国标准信息交换码。用一个字节的7位可以表示
* ISO8859-1:拉丁码表。欧洲码表用一个字节的8位表示。
* GB2312:中国的中文编码表。最多两个字节编码所有字符
* GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
* Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
* UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
*
*
*
* @author xq
* @create 2021-01-03-16:02
*/
public class InputStreamReaderTest {
/*
此时处理异常的话,仍然应该使用try-catch-finally
InputStreamReader的使用,实现字节的输入流到字符的输入流的转换
*/
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("dpcp.txt");
//参数2指明了字符集,具体使用哪个字符集,取决于文件dbcp.txt保存时使用的字符集
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用系统默认字符集
char[] cbuf = new char[1024];
int len;
while ((len = isr.read(cbuf)) != -1){
String str = new String(cbuf,0,len);
System.out.println(str);
}
isr.close();
}
/*
综合使用InputStreamReader和OutputStreamWriter
*/
@Test
public void test2() throws IOException {
File file1 = new File("dpcp.txt");
File file2 = new File("dpcp_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1){
osw.write(cbuf,0,len);
}
isr.close();
osw.close();
}
}
13.6 标准输入、输出流(了解)
package com.atguigu.java;
import org.junit.Test;
import java.io.*;
/**
* 其他流的使用
* 1.标准的输入、输出流
* 2.打印流
* 3.数据流
*
* @author shkstart
* @create 2019 下午 6:11
*/
public class OtherStreamTest {
/*
1.标准的输入、输出流
1.1
System.in:标准的输入流,默认从键盘输入
System.out:标准的输出流,默认从控制台输出
1.2
System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入和输出的流。
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();
}
}
}
}
/*
2. 打印流:PrintStream 和PrintWriter
2.1 提供了一系列重载的print() 和 println()
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.print((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() throws IOException {
//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();
}
/*
将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
注意点:读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致!
*/
@Test
public void test4() throws IOException {
//1.
DataInputStream 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);
//3.
dis.close();
}
}
13.7 打印流(了解)
实现将基本数据类型的数据格式转化为字符串输出
- 打印流:PrintStream和PrintWriter
- 提供了一系列重载的print()和println()方法,用于多种数据类型的输出
- PrintStream和PrintWriter的输出不会抛出IOException异常
- PrintStream和PrintWriter有自动flush功能
- PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。
在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。 - System.out返回的是PrintStream的实例
13.8 数据流
为了方便地操作Java语言的基本数据类型和String的数据,可以使用数据流。
- 数据流有两个类:(用于读取和写出基本数据类型、String类的数据)
- DataInputStream 和 DataOutputStream
- 分别“套接”在 InputStream 和 OutputStream 子类的流上
- DataInputStream中的方法
boolean readBoolean() byte readByte()
char readChar() float readFloat()
double readDouble() short readShort()
long readLong() int readInt()
String readUTF() void readFully(byte[] b) - DataOutputStream中的方法
- 将上述的方法的read改为相应的write即可。

京公网安备 11010502036488号