IO流
* IO: Input(输入) , Output(输出)
* 注意: 我们说输入和输出问题是站在内存的角度而言
* IO流的分类
** 按照流向进行划分
输入流 , 输出流
** 按照操作数据的类型划分
字节流 , 字符流
字节流
字节输入流 InputStream 读
字节输出流 OutputStream 写
字符流
字符输入流 Reader 读
字符输出流 Writer 写
关于字符流:
字符流的出现是为了方便的操作中文,因为我们都知道一个中文是两个字节,那么如果使用字节流进行操作的话,需要将数据进行分割,然后在进行合并比较麻烦.
什么情况下使用哪种流呢?
如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。
如果无法判断的话,就用字节流
* 基本流
FileInputStream
* 构造方法
public FileInputStream(String name )
public FileInputStream(File file)
* 成员方法
public int read() 一次读取一个字节
public int read(byte[] bytes) 一次读取一个字节数组
FileOutputStream
* 构造方法
public FileOutputStream(String name )
public FileOutputStream(File file)
public FileOutputStream(String name , boolean append)
public FileOutputStream(File file , boolean append)
* 成员方法
public void write(int by) 一次写一个字节
public void write(byte[] bytes) 一次写一个字节数组
public void write(byte[] bytes , int offset , int len) 一次写一个字节数组中的一部分
* 缓冲字节流对象
** 高效的字节输入流对象 BufferedInputStream
* 构造方法: public BufferedInputStream(InputStream in)
** 高效的字节输出流对象 BufferedOutputStream
* 构造方法 public BufferedOutputStream(OutputStream out)
下面写一个简单的I/O流的程序,比较一下四种I/O操作方式的效率
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 编写程序,采用多种方式,把E:\\cc.mp3的内容复制到F:\\cc.mp3中
方式1:基本字节流一次读写一个字节
方式2:基本字节流一次读写一个字节数组
方式3:高效字节流一次读写一个字节
方式4:高效字节流一次读写一个字节数组
*/
public class InputOutPutDemo {
public static void main(String[] args) throws IOException{
//获取方法运行前时间
long millis = System.currentTimeMillis();
//运行方法
//方式1:基本字节流一次读写一个字节
baseInputOutputFun();
//使用该I/O流方式消耗的时间为:192830
//方式2:基本字节流一次读写一个字节数组
//baseInputOutputFun2();
//使用该I/O流方式消耗的时间为:311
//方式3:高效字节流一次读写一个字节
//bufferInputOutputFun();
//使用该I/O流方式消耗的时间为:958
//方式4:高效字节流一次读写一个字节数组
//bufferInputOutputFun2();
//使用该I/O流方式消耗的时间为:112
//获取方法运行结束时间
long millis2 = System.currentTimeMillis();
//获得程序运行时间
System.out.println("使用该I/O流方式消耗的时间为:"+(millis2-millis));
}
//封装方法:基本字节流,一次读写一个字节数组
public static void baseInputOutputFun() throws IOException{
//创建输入字节流
FileInputStream fileIn = new FileInputStream("E:\\cc.mp3");
//创建输出字节流
FileOutputStream fileOu = new FileOutputStream("F:\\cc.mp3");
int by = 0;
while((by=fileIn.read())!=-1){
fileOu.write(by);
}
//关闭输出字节流
fileOu.close();
//关闭输入字节流
fileIn.close();
}
//封装方法:方式2:基本字节流一次读写一个字节数组
public static void baseInputOutputFun2() throws IOException{
//创建输入字节流
FileInputStream fileIn = new FileInputStream("E:\\cc.mp3");
//创建输出字节流
FileOutputStream fileOu = new FileOutputStream("F:\\cc.mp3");
//定义字节数组
byte[] bytes = new byte[1024];
//定义len,为读取到的实际字节长度
int len = 0;
while((len = fileIn.read(bytes))!=-1){
fileOu.write(bytes, 0, len);
}
//关闭输出字节流
fileOu.close();
//关闭输入字节流
fileIn.close();
}
//封装方法:方式3:高效字节流一次读写一个字节
public static void bufferInputOutputFun() throws IOException{
//创建高效输入流
BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream("E:\\cc.mp3"));
//创建高效输出流
BufferedOutputStream fileOu = new BufferedOutputStream(new FileOutputStream("F:\\cc.mp3"));
//一次读取一个字节复制
int by = 0;
while((by = fileIn.read())!=-1){
fileOu.write(by);
}
//关闭高效输出流
fileOu.close();
//关闭高效输入流
fileIn.close();
}
//封装方法方式4:高效字节流一次读写一个字节数组
public static void bufferInputOutputFun2() throws IOException{
//创建高效输入流
BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream("E:\\cc.mp3"));
//创建高效输出流
BufferedOutputStream fileOu = new BufferedOutputStream(new FileOutputStream("F:\\cc.mp3"));
//一次读取一个字节数组
byte[] by = new byte[1024];
//定义len为实际读到的长度
int len = 0;
while((len = fileIn.read(by))!=-1){
fileOu.write(by, 0, len);
}
//关闭高效输出流
fileOu.close();
//关闭高效输入流
fileIn.close();
}
}
使用高效流模拟copy文件夹
package cn.test2;
/*
*需求:模拟copy命令,复制文件夹
*分析:
* a: 把E:\\code2这个目录封装成一个File对象 , 把F:\\code2这个目录封装成一个File对象
* b: 判断F:\\code2这个目录否存在,如果不存在就创建出来
* c: 获取E:\\code2这个目录中所有的文件对应的File数组
* d: 遍历数组,获取每一个元素,然后判断
* 1>File对象为文件夹,则递归(执行b)
* 2>File对象为文件,则复制
* (1): 创建目标文件
* (2): 创建高效字节输入流和高效的字节输出流对象
* (3): 频繁的读写操作
* (4): 释放资源
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFolderTest {
public static void main(String[] args) throws IOException {
//将E:\\code2这个文件夹封装为File对象
File srcFolder = new File("E:\\code2");
//将F:\\code2这个文件夹封装为File对象
File destFolder = new File("F:\\code2");
//复制文件夹
copyFolder(srcFolder,destFolder);
}
//复制文件夹方法
public static void copyFolder(File srcFolder, File destFolder) throws IOException {
// TODO Auto-generated method stub
//判断destFolder这个文件夹是否存在
if(!destFolder.exists()){
destFolder.mkdirs();
}
//获取srcFolder文件夹下的文件对象
File[] Files = srcFolder.listFiles();
//遍历
for(File File:Files){
//创建目标文件
String name = File.getName();
File destFile = new File(destFolder,name);
//如果File对象为文件对象
if(File.isFile()){
//复制文件
copyFile(File,destFile);
}
//File对象为文件夹对象
else if(File.isDirectory()){
//递归调用文件夹复制方法
copyFolder(File, destFile);
}
}
}
public static void copyFile(File file, File destFile) throws IOException {
// TODO Auto-generated method stub
//创建高效输入字节流对象
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
//创建高效输出字节流对象
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bytes = new byte[1024];
int len = 0;
while((len = br.read(bytes))!=-1){
bw.write(bytes, 0, len);
}
bw.close();
br.close();
}
}