IO流
IO,即in和out,也就是输入和输出,Java中一般使用在文件的上传和下载。
流,是一个抽象概念,是指一连串的数据(字节或字符),在读取的时候,会将文件变成流的形式,来进行传输。
缓冲
缓冲区又称为缓存,它是内存空间的一部分。
也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的空间就叫做缓冲区。
使用缓冲能够提高工作效率。
IO流的分类
输入流和输出流
IO流按照数据的传输方向
可以分为:输入流和输出流
。
所谓的输入和输出都是相对于应用程序(Java代码)而言的。如下图:
输入流:从数据源(磁盘)到Java代码
输出流:从Java代码到数据源(磁盘)
输入流分为两种:
1、字节输入流 (FileInputStream)
2、字符输入流
输出流也分为两种:
1、字节输出流 (FileOutputStream)
2、字符输出流
字节输入流
字节输入流FileInputStream
主要方法:
返回值 | 方法名 | 详情 |
---|---|---|
int | read() | 从此输入流中读取一个数据字节。 |
int | read(byte[] b) | 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。 |
int | read(byte[] b, int off, int len) | 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。 |
void | close() | 关闭此输入流并释放与该流关联的所有系统资源。 |
对于IO流来讲,代码的书写有着固定的格式,无论是输入和输出。
package com.lyc.lesson;
import java.io.*;
public class Lesson01 {
public static void main(String[] args) throws IOException {
//1、获取文件
File file = new File("C:/Users/ASUS/Desktop/zz2212/aaa/3.txt");
//2、将文件转化为文件流
InputStream fis = new FileInputStream(file);
//3、创建缓冲
BufferedInputStream bis = new BufferedInputStream(fis);
//4、创建缓冲区数组
//new byte[2] 每次读取两个字节
byte[] bytes = new byte[2];
//5、将缓冲区的字节存入到数组中
int read = bis.read(bytes);
//6、输出数组中的数据
// System.out.println(new String(bytes,0,read));
int length = -1;
while ((length = bis.read(bytes)) != -1){
System.out.println("-------------");
System.out.println(new String(bytes,0,length));
}
//7、关闭流
bis.close();
fis.close();
}
}
由于有缓冲和无缓冲效率不一样,所以这里我们采用效率高的有缓冲的方式来写代码。无缓冲的可自行查找。
字节输出流
字节输出流FileOutputStream
主要方法:
返回值 | 方法名 | 详情 |
---|---|---|
void | write(byte[] b) | 将 b.length 个字节从指定 byte 数组写入此文件输出流中。 |
void | write(byte[] b, int off, int len) | 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 |
void | write(int b) | 将指定字节写入此文件输出流。 |
void | close() | 关闭此输入流并释放与该流关联的所有系统资源。 |
输出流也是固定的格式,只要熟练掌握即可。
package com.lyc.lesson;
import java.io.*;
public class Lesson04 {
public static void main(String[] args) throws IOException {
//获取文件位置
File file = new File("C:/Users/ASUS/Desktop/zz2212/aaa/1.txt");
//
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//将字符串转化为字节数组
String str = "我是帅哥,你好帅";
byte[] bytes1 = str.getBytes();
//write(),将字符数组写入缓冲数组中
bos.write(bytes1,0, bytes1.length);
//刷新
bos.flush();
//关闭流,先创建的流,后关闭;后创建的流,先关闭
bos.close();
fos.close();
}
}
字节输入和输出的综合案例
需求:将一个文件复制到另一个文件中。
思路:现将文件读取到内存中,然后再从内存中将文件写入到另一个文件中。
package com.lyc.lesson;
import java.io.*;
public class Lesson05 {
public static void main(String[] args) throws IOException {
/**
* 将1.txt中的内容,复制到2.txt文件中
*
*/
//输入
File file = new File("C:/Users/ASUS/Desktop/zz2212/aaa/1.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
//输出
File file1 = new File("C:/Users/ASUS/Desktop/zz2212/aaa/2.txt");
FileOutputStream fos = new FileOutputStream(file1);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[4*1024];
int length;
while ((length = bis.read(bytes)) != -1){
//将读到的字节,写入到另一个输出缓冲区中
bos.write(bytes,0,length);
}
bos.flush();
bos.close();
bis.close();
fos.close();
fis.close();
}
}
注意:
在案例中,我们一方面要注意路径问题,另一方面,在输出流中,我们同样要创建一个空的对应的文件。如下,虽然我们在另一个文件夹没有这个文件,但是我们要创建一个空的文件来接收读取到字节流
。
字符输出流【非重点】
也是输入流,将磁盘的某一个文件读取到内存
FileReader:
是一个阅读字符文件的便利类,是专门处理字符文件的,比如txt文件。音频视频图片
是从字节流到字符流的桥:它读取字节,并使用指定的charset
将其解码为字符 。 它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
牵涉到解码,底层是字节流,但是会解码为字符。如果解码失败就意味着咱们读取失败了
一般不会使用字符流操作图片 音频 视频等,因为牵涉到解码。会出问题!!!
开发一般使用字节流!!!
package com.lyc.lesson;
import java.io.*;
public class Lesson06 {
public static void main(String[] args) throws IOException {
//将c盘下面的aaa文件夹下面的1.txt文件的内容 读取到Java内存中
//和字节流一模一样
//1.新建一个File对象
File file = new File("c:/aaa/1.txt");
//2.新建FileReader对象 字符输入流
FileReader fr = new FileReader(file);
//3.FileRFeader没有缓冲的效果,可以加上缓冲的效果
BufferedReader br = new BufferedReader(fr);
//4.字节的缓冲数组 是字节
//字符的缓冲数组 是字符
char[] cbuf = new char[4];//缓冲区是4个字符
//5. read(char[] cbuf) 将文件内容读取到字符数组中(缓冲区)
int length = -1;
while ((length = br.read(cbuf)) != -1) {
System.out.println(length);
//6.将数组展示到控制台
System.out.println(new String(cbuf, 0, length));
}
//7.关闭流
br.close();
fr.close();
}
}
字符输出流
package com.lyc.lesson;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Lesson07 {
public static void main(String[] args) throws IOException {
//从内存写入数据到磁盘的文件中
File file = new File("c:/aaa/2.txt");
FileWriter fw = new FileWriter(file);
//加缓冲流
BufferedWriter bw = new BufferedWriter(fw);
//viod writer(char[] cbuf, int off, int length);
String str = "今天的天气真的太热了";
//将字符串转为char数组
char[] chars = str.toCharArray();
//将chars数组中 从0 开始 取3个字符写入到缓冲流中
bw.write(chars, 0, 3);
bw.close();
fw.close();
}
}
字符输入和输出的综合案例:
需求:
复制一个纯文本到另一个文件中。
package com.lyc.lesson;
import java.io.*;
public class lesson08 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(new File("c:/bbb/DiBa.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("c:/aaa/sb1.txt")));
int length = -1;
char[] cbuf = new char[4 * 1024];
while ((length = br.read(cbuf)) != -1) {
bw.write(cbuf, 0, length);
}
bw.close();
br.close();
}
}
iter(new FileWriter(new File(“c:/aaa/sb1.txt”)));
int length = -1;
char[] cbuf = new char[4 * 1024];
while ((length = br.read(cbuf)) != -1) {
bw.write(cbuf, 0, length);
}
bw.close();
br.close();
}
}