一、流的分类
- 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
- 按数据流的流向不同分为:输入流,输出流
- 按流的角色的不同分为:节点流,处理流
二、流体系
四大抽象基类:
流体系:
注意:
我们需要掌握的有16大流:(4个抽象类+12实现类)
抽象基类:InputStream OutputStream Reader Writer
文件流(节点流):FileInputStream FileOutputStream FileReader FileWriter
缓冲流:BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
转换流: InputStreamReader OutputStreamWriter
对象流:ObjectInputStream ObjectOutputStream
三、12大实现类
文本文件:(txt、java、c、cpp):使用字符流处理
非文本文件(.jpg,mp3,avi,doc,ppt):使用字节流处理
字节流(复制test.mp4:12分13秒)
1、InputStream OutputStream(节点流或文件流):215毫秒
public class InputStreamReaderTest {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Dell\\Desktop\\test.txt";
String destPath = "C:\\Users\\Dell\\Desktop\\testCopy.txt";
copyFile(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("复制操作所花费的时间为:" + (end - start));//215毫秒
}
public static void copyFile(String srcPath, String destPath) {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
isr = new InputStreamReader(new FileInputStream(srcFile), "utf-8");
osw = new OutputStreamWriter(new FileOutputStream(destFile), "gbk");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
osw.write(cbuf, 0, len);
}
osw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (osw != null)
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、BufferedInputStream BufferedOutputStream(缓存流):60毫秒
public void copyFile(String srcPath, String destPath) {
BufferedOutputStream bo = null;
BufferedInputStream bi = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
bi = new BufferedInputStream(new FileInputStream(srcFile));
bo = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bytes = new byte[1024];
int len;
while ((len = bi.read(bytes)) != -1) {
bo.write(bytes);
}
bo.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bo != null)
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bi != null)
bi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void test() {
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Dell\\Desktop\\test.mp4";
String destPath = "C:\\Users\\Dell\\Desktop\\testCopy.mp4";
copyFile(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("复制操作所花费的时间为:" + (end - start));//60毫秒
}
字符流(test.txt:10591个字)
1、FileReader FileWriter(文件流或节点流):10毫秒
public class FileReaderWriterTest {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Dell\\Desktop\\test.txt";
String destPath = "C:\\Users\\Dell\\Desktop\\testCopy2.txt";
copyFile(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("复制操作所花费的时间为:" + (end - start));//5毫秒
}
public static void copyFile(String srcPath, String destPath) {
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
//1.创建File类对象:指明输入文件和输出文件
//被输出文件
File srcFile = new File(srcPath);
//被输入文件
File destFile = new File(destPath);
//2.创建输入流和输出流对象
fileReader = new FileReader(srcFile);
fileWriter = new FileWriter(destFile);
//3.数据的读入和写出操作
char[] cbuf = new char[10];
int len;
while ((len = fileReader.read(cbuf)) != -1) {
fileWriter.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭资源
try {
if (fileWriter != null) {
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、BufferedReader BufferedWriter(缓存流):2毫秒
public class BufferedWriterReaderTest {
@Test
public void test() {
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\Dell\\Desktop\\test.txt";
String destPath = "C:\\Users\\Dell\\Desktop\\testCopy.txt";
copyFile(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("复制操作所花费的时间为:" + (end - start));//215毫秒
}
public void copyFile(String srcPath, String destPath) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
br = new BufferedReader(new FileReader(srcFile));
bw = new BufferedWriter(new FileWriter(destFile));
String str;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转换流
1、 InputStreamReader :将InputStream转换为Reader
实现将字节的输入流按指定字符集转换为字符的输入流
2、OutputStreamWriter:将Writer转换为OutputStream
实现将字符的输出流按指定字符集转换为字节的输出流。
3.代码
public static void copyFile(String srcPath, String destPath) {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
isr = new InputStreamReader(new FileInputStream(srcFile), "utf-8");
osw = new OutputStreamWriter(new FileOutputStream(destFile), "gbk");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
osw.write(cbuf, 0, len);
}
osw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (osw != null)
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
对象流
1、ObjectOutputStream:对象序列化
//序列化
@Test
public void ObjectOutputStreamTest() {
ObjectOutputStream oos = null;
try {
File destFile = new File("object.dat");
oos = new ObjectOutputStream(new FileOutputStream(destFile));
oos.writeObject(new String("我爱祖国!!!"));
oos.flush();
oos.writeObject(new Person("张三", 22));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、ObjectInputStream :对象反序列化
//反序列化
@Test
public void ObjectInputStreamTest() {
ObjectInputStream ois = null;
try {
File srcFile = new File("object.dat");
ois = new ObjectInputStream(new FileInputStream(srcFile));
Object obj = ois.readObject();
String str = (String) obj;
Person p = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}