IO流:IO用于在设备间进行数据传输的操作
IO流的分类:
注意:
(1)如果我们没有明确说明按照什么分,默认按照数据类型分。
(2)除非文件用windows自带的记事本打开我们能够读懂,才采用字符流,否则建议使用字节流。
IO流(图解):
一、字节流
OutputStream
数据读写--读取(FileOutputStream):
字节输出流操作步骤:
- 创建字节输出流对象
- 调用write()方法
- 释放资源
需求:我要往一个文本文件中输入一句话:"hello,io"
* 需求:我要往一个文本文件中输入一句话:"hello,io"
*
* 分析:
* A:这个操作最好是采用字符流来做,但是呢,字符流是在字节流之后才出现的,所以,今天我先讲解字节流如何操作。
* B:由于我是要往文件中写一句话,所以我们要采用字节输出流。
*
* 通过上面的分析后我们知道要使用:OutputStream
* 但是通过查看API,我们发现该流对象是一个抽象类,不能实例化。
* 所以,我们要找一个具体的子类。
* 而我们要找的子类是什么名字的呢?这个时候,很简单,我们回想一下,我们是不是要往文件中写东西。
* 文件是哪个单词:File
* 然后用的是字节输出流,联起来就是:FileOutputStream
* 注意:每种基类的子类都是以父类名作为后缀名。
* XxxOutputStream
* XxxInputStream
* XxxReader
* XxxWriter
* 查看FileOutputStream的构造方法:
* FileOutputStream(File file)
* FileOutputStream(String name)
*
* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:写数据
* C:释放资源
*/
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// FileOutputStream(File file)
// File file = new File("fos.txt");
// FileOutputStream fos = new FileOutputStream(file);
// FileOutputStream(String name)
FileOutputStream fos = new FileOutputStream("fos.txt");
/*
* 创建字节输出流对象了做了几件事情:
* A:调用系统功能去创建文件
* B:创建fos对象
* C:把fos对象指向这个文件
*/
//写数据
fos.write("hello,IO".getBytes());
fos.write("java".getBytes());
//释放资源
//关闭此文件输出流并释放与此流有关的所有系统资源。
fos.close();
/*
* 为什么一定要close()呢?
* A:让流对象变成垃圾,这样就可以被垃圾回收器回收了
* B:通知系统去释放跟该文件相关的资源
*/
//java.io.IOException: Stream Closed
//fos.write("java".getBytes());
}
}
案例:数据的写入(Write)
Write() 方法:
- public void write(int b):写一个字节
- public void write(byte[] b):写一个字节数组
- public void write(byte[] b,int off,int len):写一个字节数组的一部分
/*
* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:调用write()方法
* C:释放资源
*
* public void write(int b):写一个字节
* public void write(byte[] b):写一个字节数组
* public void write(byte[] b,int off,int len):写一个字节数组的一部分
*/
public class FileOutputStreamDemo2 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// OutputStream os = new FileOutputStream("fos2.txt"); // 多态
FileOutputStream fos = new FileOutputStream("fos2.txt");
// 调用write()方法
//fos.write(97); //97 -- 底层二进制数据 -- 通过记事本打开 -- 找97对应的字符值 -- a
// fos.write(57);
// fos.write(55);
//public void write(byte[] b):写一个字节数组
byte[] bys={97,98,99,100,101};
fos.write(bys);
//public void write(byte[] b,int off,int len):写一个字节数组的一部分
fos.write(bys,1,3);
//释放资源
fos.close();
}
}
案例:
- 如何实现数据的换行?
- 如何实现数据的追加写入?
/*
* 如何实现数据的换行?
* 为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。
* 如何实现呢?写入换行符号即可呗。
* 刚才我们看到了有写文本文件打开是可以的,通过windows自带的那个不行,为什么呢?
* 因为不同的系统针对不同的换行符号识别是不一样的?
* windows:\r\n
* linux:\n
* Mac:\r
* 而一些常见的个高级记事本,是可以识别任意换行符号的。
*
* 如何实现数据的追加写入?
* 用构造方法带第二个参数是true的情况即可
*/
public class FileOutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// 创建字节输出流对象
// FileOutputStream fos = new FileOutputStream("fos3.txt");
// 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
FileOutputStream fos = new FileOutputStream("fos3.txt", true);
// 写数据
for (int x = 0; x < 10; x++) {
fos.write(("hello" + x).getBytes());
fos.write("\r\n".getBytes());
}
// 释放资源
fos.close();
}
}
案例:加入异常处理的字节输出流操作
/*
* 加入异常处理的字节输出流操作
*/
public class FileOutputStreamDemo4 {
public static void main(String[] args) {
// 改进版
// 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值
FileOutputStream fos = null;
try {
// fos = new FileOutputStream("z:\\fos4.txt");
fos = new FileOutputStream("fos4.txt");
fos.write("java".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 如果fos不是null,才需要close()
if (fos != null) {
// 为了保证close()一定会执行,就放到这里了
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
InputStream--数据读写--写出
读取数据的方式:
- int read():一次读取一个字节
- int read(byte[] b):一次读取一个字节数组
- 每次可以读取多个数据,调高了操作效率
两种方法的图解:
1、字节流复制文件( int read() ):
/*
* 复制文本文件为例。
*
* 数据源:从哪里来
* a.txt -- 读取数据 -- FileInputStream
*
* 目的地:到哪里去
* b.txt -- 写数据 -- FileOutputStream
*
* java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
*
* 这一次复制中文没有出现任何问题,为什么呢?
* 上一次我们出现问题的原因在于我们每次获取到一个字节数据,就把该字节数据转换为了字符数据,然后输出到控制台。
* 而这一次呢?确实通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,你没有做任何的转换。
* 它会自己做转换。
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("b.txt");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源(先关谁都行)
fos.close();
fis.close();
}
}
2、字节流复制文件( int read( byte[] b) ):
/*
* 需求:把c:\\a.txt内容复制到d:\\b.txt中
*
* 数据源:
* c:\\a.txt -- 读取数据 -- FileInputStream
* 目的地:
* d:\\b.txt -- 写出数据 -- FileOutputStream
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("c:\\a.txt");
FileOutputStream fos = new FileOutputStream("d:\\b.txt");
// 复制数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 释放资源
fos.close();
fis.close();
}
}
字节缓冲区流:缓冲区类(高效类)
- 写数据:BufferedOutputStream
- 读数据:BufferedInputStream
(1)BufferedOutputStream:
/* 构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
*
* 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
* 原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。
*/
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedOutputStream(OutputStream out)
// FileOutputStream fos = new FileOutputStream("bos.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fos);
// 简单写法
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt"));
// 写数据
bos.write("hello".getBytes());
// 释放资源
bos.close();
}
}
(2)BufferedInputStream:
/*
* 注意:虽然我们有两种方式可以读取,但是,请注意,这两种方式针对同一个对象在一个代码中只能使用一个。
*/
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"bos.txt"));
// 读取数据
// int by = 0;
// while ((by = bis.read()) != -1) {
// System.out.print((char) by);
// }
// System.out.println("---------");
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
// 释放资源
bis.close();
}
}
字节流四种方式复制文件:
- 基本字节流一次读写一个字节: 共耗时:117235毫秒
- 基本字节流一次读写一个字节数组: 共耗时:156毫秒
- 高效字节流一次读写一个字节: 共耗时:1141毫秒
- 高效字节流一次读写一个字节数组: 共耗时:47毫秒
需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1("e:\\哥有老婆.mp4", "copy1.mp4");
// method2("e:\\哥有老婆.mp4", "copy2.mp4");
// method3("e:\\哥有老婆.mp4", "copy3.mp4");
method4("e:\\哥有老婆.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
// 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
public static void method1(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
二、字符流(转换流)
1、OutputStreamWriter(编码转换)
- OutputStreamWriter(OubiantputStream out): 根据默认编码把字节流数据 --> 字符流
- OutputStreamWriter(OutputStream out,String charsetName): 根据指定编码把字节流数据 --> 字符流
把字节流转换为字符流:字符流 = 字节流 + 编码表
public class OutputStreamWriterDemo {
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的方法:
- 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):写一个字符串的一部分
/* 面试题:close()和flush()的区别?
* A:close()关闭流对象,但是先刷新一次缓冲区。关闭之后,流对象不可以继续再使用了。
* B:flush()仅仅刷新缓冲区,刷新之后,流对象还可以继续使用。
*/
public class OutputStreamWriterDemo {
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);
}
}
1、InputStreamWriter(编码转换)
- InputStreamReader(InputStream is):用默认的编码读取数据
- InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
/*
* InputStreamReader(InputStream is):用默认的编码读取数据
* InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
*/
public class InputStreamReaderDemo {
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();
}
}
2、InputStreamReader的方法:
- int read():一次读取一个字符
- int read(char[] chs):一次读取一个字符数组
/*
* InputStreamReader的方法:
* int read():一次读取一个字符
* int read(char[] chs):一次读取一个字符数组
*/
public class InputStreamReaderDemo {
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();
}
}
案例1(字符流复制文本文件):
需求:把当前项目目录下的a.txt内容复制到当前项目目录下的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();
}
}
转换流的简化写法:
/*
* 由于我们常见的操作都是使用本地默认编码,所以,不用指定编码。
* 而转换流的名称有点长,所以,Java就提供了其子类供我们使用。
* OutputStreamWriter = FileOutputStream + 编码表(GBK)
* FileWriter = FileOutputStream + 编码表(GBK)
*
* InputStreamReader = FileInputStream + 编码表(GBK)
* FileReader = FileInputStream + 编码表(GBK)
*
/*
* 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
*
* 数据源:
* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader
* 目的地:
* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter
*/
public class CopyFileDemo2 {
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();
}
}
字符缓冲流(BufferWriter):
字符流为了高效读写,也提供了对应的字符缓冲流。
- BufferedWriter: 字符缓冲输出流
- BufferedReader:字符缓冲输入流
1、BufferedWriter:字符缓冲输出流:
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
可以指定缓冲区的大小,或者接受默认的大小。在大多数情况下,默认值就足够大了。
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
// BufferedWriter(Writer out)
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream("bw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
bw.write("hello");
bw.write("world");
bw.write("java");
bw.flush();
bw.close();
}
}
2、BufferedReader:
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
// 创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
// 方式1
int ch = 0;
while ((ch = br.read()) != -1) {
System.out.print((char) ch);
}
// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}
// 释放资源
br.close();
}
}
案例(字符缓冲流复制文本文件):
/*
* 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
*
* 数据源:
* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
// 方法一
// 两种方式其中的一种一次读写一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
bw.flush();
}
// 方法二
// 读写数据
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
// 释放资源
bw.close();
br.close();
}
}
字符缓冲流的(特殊方法):
- BufferedWriter:
- BufferedReader:
/*
* 字符缓冲流的特殊方法:
* BufferedWriter:
* public void newLine():根据系统来决定换行符
* BufferedReader:
* public String readLine():一次读取一行数据
* 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
*/
public class BufferedDemo {
public static void main(String[] args) throws IOException {
// write();
read();
}
private static void read() throws IOException {
// 创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw2.txt"));
// public String readLine():一次读取一行数据
// String line = br.readLine();
// System.out.println(line);
// line = br.readLine();
// System.out.println(line);
// 最终版代码
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//释放资源
br.close();
}
private static void write() throws IOException {
// 创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("bw2.txt"));
for (int x = 0; x < 10; x++) {
bw.write("hello" + x);
// bw.write("\r\n");
bw.newLine();
bw.flush();
}
bw.close();
}
}
*案例(字符流复制文本文件):
/*
* 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
*
* 数据源:
* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
// 读写数据
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
// 释放资源
bw.close();
br.close();
}
}
IO小结: