1.IO流概述2.字节流
2.1 字节输出流:写数据2.2 字节输入流:读数据2.3 案例:复制文件 3.字节缓冲流
1.IO流概述IO 的数据传输,可以看做是一种数据的流动,按照流动的方向,以内存为参照物,进行读写操作。简单来说,内存在读,内存在写。
按流向分
按数据类型分
什么是纯文本文件?
用 windows 记事本打开能读的懂,那么这样的文件就是纯文本文件。
2.字节流 2.1 字节输出流:写数据示例1:一次写一个字节数据
package com.qdu;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo1 {
public static void main(String[] args) throws IOException {
// 1.创建字节输出流的对象 --- 告诉虚拟机我要往哪个文件中写数据
FileOutputStream fos = new FileOutputStream("G:\a.txt");
// FileOutputStream fos = new FileOutputStream(new File("D:\a.txt"));
// 2.写数据 --- 传递一个整数时,那么实际上写到文件中的是这个整数在码表中对应的那个字符
fos.write(98);
// 3.释放资源 --- 告诉操作系统我现在已经不用这个文件了
fos.close();
}
}
示例2:一次写一个字节数组数据
package com.qdu;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo4 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("bytestream\a.txt");
byte[] bys = {97, 98, 99};
fos.write(bys);
fos.close();
}
}
示例3:一次写一个字节数组的部分数据
package com.qdu;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo4 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("bytestream\a.txt");
byte[] bys = {97, 98, 99, 100, 101, 102, 103};
fos.write(bys, 1, 2);
fos.close();
}
}
字节流写数据如何实现换行呢?
写完数据后,加换行符:
windows:rnlinux:nmac:r
字节流写数据如何实现追加写入呢?
public FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件。如果第二个参数为 true,不会清空文件里面的内容。
package com.qdu;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo5 {
public static void main(String[] args) throws IOException {
// 第二个参数默认是false,表示不打开续写功能,那么创建对象的这行代码会清空文件
// 如果第二个参数为true,表示打开续写功能,那么创建对象的这行代码不会清空文件
FileOutputStream fos = new FileOutputStream("bytestream\a.txt", true);
fos.write(97);
fos.write("rn".getBytes());
fos.write(98);
fos.write("rn".getBytes());
fos.write(99);
fos.write("rn".getBytes());
fos.write(100);
fos.write("rn".getBytes());
fos.write(101);
fos.write("rn".getBytes());
fos.close();
}
}
字节流写数据加try…catch异常处理
finally:在异常处理时提供 finally 块来执行所有清除操作,比如 IO 流中的释放资源。
特点:被 finally 控制的语句一定会执行,除非 JVM 退出。
异常处理标准格式:try…catch…finally
package com.qdu;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo6 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("D:\a.txt");
fos.write(97);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.2 字节输入流:读数据
示例1:读一个字节
package com.qdu;
import java.io.FileInputStream;
import java.io.IOException;
public class OutputDemo7 {
public static void main(String[] args) throws IOException {
// 如果文件存在,那么就不会报错
// 如果文件不存在,那么就直接报错
FileInputStream fis = new FileInputStream("bytestream\a.txt");
int read = fis.read();
// 一次读取一个字节,返回值就是本次读到的那个字节数据,也就是字符在码表中对应的那个数字
// 如果我们想要看到的是字符数据,那么一定要强转成char
System.out.println((char)read);
// 释放资源
fis.close();
}
}
示例2:读多个字节
package com.qdu;
import java.io.FileInputStream;
import java.io.IOException;
public class OutputDemo8 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("bytestream\a.txt");
// 读文件中多个字节
int b;
while ((b = fis.read()) != -1) {
System.out.println((char) b);
}
fis.close();
}
}
2.3 案例:复制文件
练习1:字节流一次读写一个字节
package com.qdu;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo9 {
public static void main(String[] args) throws IOException {
// 创建字节输入流,准备读数据
FileInputStream fis = new FileInputStream("C:\itheima\a.avi");
// 创建字节输出流,准备写数据
FileOutputStream fos = new FileOutputStream("bytestream\a.avi");
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
}
练习2:字节流一次读写一个字节数组
为了解决速度问题,字节流通过创建字节数组,可以一次读写多个数据。
一次读一个字节数组的方法:
public int read(byte[] b) 从输入流读取最多 b.length 个字节的数据,返回的是读入缓冲区的总字节数,也就是实际的读取字节个数。
package com.qdu;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputDemo10 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:\itheima\a.avi");
FileOutputStream fos = new FileOutputStream("bytestream\a.avi");
byte[] bytes = new byte[1024];
int len; // 本次读到的有效字节个数 --- 这次读了几个字节
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
}
}
3.字节缓冲流
字节缓冲输出流:BufferOutputStream
构造方法:BufferedOutputStream(OutputStream out)
字节缓冲输入流:BufferedInputStream
构造方法:BufferedInputStream(InputStream in)
为什么构造方法需要的是字节流,而不是具体的文件或者路径呢?
字节缓冲流仅仅提供缓冲区,而真正的读写数据还得依靠基本的字节流对象进行操作
练习3:字节缓冲流一次操作一个字节
package com.qdu;
import java.io.*;
public class OutputDemo11 {
public static void main(String[] args) throws IOException {
// 创建一个字节缓冲输入流,在底层创建了一个默认长度为8192的字节数组
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bytestream\a.avi"));
// 创建一个字节缓冲输出流,在底层也创建了一个默认长度为8192的字节数组
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bytestream\copy.avi"));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
// 方法的底层会把字节流给关闭
bis.close();
bos.close();
}
}
练习4:字节缓冲流一次操作一个字节数组
package com.qdu;
import java.io.*;
public class OutputDemo12 {
public static void main(String[] args) throws IOException {
// 创建一个字节缓冲输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bytestream\a.avi"));
// 创建一个字节缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bytestream\copy.avi"));
byte [] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bis.close();
bos.close();
}
}



