1.1 IO流的概述及其分类
IO流概述
IO流用来处理设备之间的数据传输
Java对数据的操作是通过流的方式
Java用于操作流的对象都在IO包中 java.io
IO流分类
a:按照数据流向 站在内存角度
输入流 读入数据
输出流 写出数据
b:按照数据类型
字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件
什么情况下使用哪种流呢?
如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。
如果你什么都不知道,就用字节流
1.2 IO流基类概述
字节流的抽象基类:
InputStream ,OutputStream。
字符流的抽象基类:
Reader , Writer
1.2.1 基本字节输入输出流的构造方法
输出流
注意事项:
创建字节输出流对象了做了几件事情?
a:调用系统资源创建a.txt文件
b:创建了一个fos对象
c:把fos对象指向这个文件
public static void main(String[] args) throws IOException {
/* 采用多种方式,把d:\\video01.avi的内容复制到d:\\video02.avi中
方式1:基本字节流一次读写一个字节
FileInputStream in = new FileInputStream("e.txt");
FileOutputStream out = new FileOutputStream("e2.txt");
int len=0;
byte[] bytes = new byte[1024];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
out.close();
in.close();
}
}
输入流
为什么一定要close()?
a: 通知系统释放关于管理a.txt文件的资源
b: 让Io流对象变成垃圾,等待垃圾回收器对其回收
public static void read() throws IOException {
BufferedInputStream bis = new BufferedInputStream("fos.txt");
byte[] buffer = new byte[5];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
bis.close();
}
}
1.2.2 FileOutputStream的三个write()方法
A:FileOutputStream的三个write()方法
public void write(int b):写一个字节 超过一个字节 砍掉前面的字节
public void write(byte[] b):写一个字节数组
public void write(byte[] b,int off,int len):写一个字节数组的一部分
B:案例演示: FileOutputStream的三个write()方法
windows下的换行符只用是 \r\n
1.2.3 FileOutputStream写出数据加入异常处理
public class MyTest2 {
public static void main(String[] args) {
//流的异常处理
FileInputStream in =null;
FileOutputStream out=null;
try {
in = new FileInputStream("e.txt");
out = new FileOutputStream("ee.txt");
//频繁的读写操作
byte[] bytes = new byte[1024 * 1024];
int len = 0;//记录每次读取到的有效字节个数
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.2.4 FileInputStream读取数据一次一个字节
案例演示:
方式1:基本字节流一次读写一个字节
public static void main(String[] args) throws IOException {
/* 采用多种方式,把d:\\video01.avi的内容复制到d:\\video02.avi中
FileInputStream in = new FileInputStream("e.txt");
FileOutputStream out = new FileOutputStream("e2.txt");
int len=0;
byte[] bytes = new byte[1024];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
out.close();
in.close();
}
}
1.2.5 字节流复制MP3
案例演示:
public static void main(String[] args) throws IOException {
BufferedInputStream bin = new BufferedInputStream(new FileInputStream("曾经的你.mp3"));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("我爱你.mp3"));
int len=0;
byte[] bytes = new byte[1024];
while ((len=bin.read(bytes))!=-1){
bout.write(bytes,0,len);
bout.flush();
}
bin.close();
bout.close();
}
}
1.2.6 FileInputStream读取数据一次一个字节数组
案例演示:
public class 字节流复制文件夹2 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("a.txt");
FileOutputStream out = new FileOutputStream("h.txt");
int len =0;
byte[] bytes = new byte[1024];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
}
}
1.2. 高效的字节输入输出流
A:缓冲思想
字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
这是加入了数组这样的缓冲区效果,java本身在设计的时候,
也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流
B:BufferedOutputStream的构造方法
查看API
BufferedOutputStream(OutputStream out)
BufferedOutputStream写出数据
public class 字节流复制文件夹4高效 {
public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("歌曲串烧.mp3"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("你爱我吗.mp3"));
int len=0;
byte[] bytes = new byte[1024 * 8];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
}
}