Java中的I/O(input/output)流是用来处理设备之间数据的传递的,用来操作流的对象都存放在JAVA.IO包中。
IO流的分类:
按流向: 输入流和输出流
按操作对象: 节符流和字节流
按功能:节点流和处理流
JAVA中所提供的所有的流类型都继承于以下四个抽象流基类:
字节流 | 字符流 | |
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
因为都是抽象的类,所以只能使用继承了基类的类来创建对象引用。
简单介绍一下:
InputStream:输入字节数据用的类。常用方法:
abstract int read() 读取一个字节并以整数形式返回(0-255),如果返回-1则到了输入流的末尾。
int read(byte [] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b
中,返回读入缓冲区的总字节数;如果因为已经到达 流末尾而不再有数据可用,则返回 -1。
int read(byte []b,int off,int len) 读取len个字节并存入字符数组b中,返回实际读取的字节数,如果读到末尾返回-1.
void close() 关闭并释放内存资源。
long skin(long n) 跳过N个字节不读,返回实际跳过的字节数。
int available() 返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。
Reader: 输入字符数据用的类
常用方法与InputStream基本相同,没有int available()方法。
OutputStream:输出字节数据用的类。常用方法:
void write(int b) 向输出流写入一个字节数据,为参数b的低8位
void write(byte [] b) 将一个字节类型数组中的数据写入输出流
void write(byte [] b,int off,int len) 将制定数组从制定位置开始到len结束的部分写入输出流
void flush() 将输出流中缓冲的数据全部写出到目的地
void close() 关闭流并释放内存资源
Writer:输出字符数据用的类
常用方法与OutputStream类似,不过Writer的Write()方法支持对字符数组、单个字符、字符串、字符串一部分、字符数组一部分的输出。
缓冲流:
缓冲流存在的前提必须是相应的流先存在,它对数据的读写提供了缓冲的功能,提高了对数据的读写效率,对流的功能进行了相应的增强。
JAVA提供了四种缓冲流:BufferedReader BufferedWriter BufferedInputStream BufferedOutputStream 。
BufferedReader 提供了readLine()方法方便用于读取一行字符串(/r /n标示)。
BufferedWriter 提供了newLine()方法用于写入一个行分隔符。
对于输出的缓冲流,写出的数据会先在缓存中存放,使用flush()可以将缓存中的数据全部读出,即刷新流。
练习:(需求:使用四种方式将C盘下一个MP3文件拷贝到D盘下)
import java.io.*;
public class CopyMp3 {
public static void main(String[] args) {
copy1();
copy2();
copy3();
copy4();
}
// 不使用缓冲区
public static void copy1() {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream("c://fir.mp3");
fo = new FileOutputStream("d://fir1.mp3");
int num = 0;
while ((num = fi.read()) != -1) {
fo.write(num);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fi != null) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 使用自定义缓冲区
public static void copy2() {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream("c://fir.mp3");
fo = new FileOutputStream("d://fir2.mp3");
int num = 0;
byte[] b = new byte[1024 * 4];
while ((num = fi.read(b)) != -1) {
fo.write(b, 0, num);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fi != null) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 使用刚好的缓冲区
public static void copy3() {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream("c://fir.mp3");
fo = new FileOutputStream("d://fir3.mp3");
byte[] b = new byte[fi.available()];
fi.read(b);
fo.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fi != null) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 使用缓冲流
public static void copy4() {
FileInputStream fi = null;
FileOutputStream fo = null;
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
try {
fi = new FileInputStream("c://fir.mp3");
fo = new FileOutputStream("d://fir4.mp3");
bi = new BufferedInputStream(fi);
bo = new BufferedOutputStream(fo);
int num = 0;
while ((num = bi.read()) != -1) {
bo.write(num);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fi != null) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}