一、流的分类
- 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
- 按数据流的流向不同分为:输入流,输出流
- 按流的角色的不同分为:节点流,处理流
(抽象基类) | 字节流 | 字符流 |
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
1.Java的IO流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的。
2.由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
操作文本文件用字符流,操作非文本文件用字节流。
二、基础掌握
FileInputStream、FileOutputStream 、FileReader、FileWriter
BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter(基于FileInputStream、FileOutputStream 、FileReader、FileWriter的操作,效率要高一些)
FileInputStream:字节流,节点流,输入流
@Test
public void testFileInputStream() {
File file = new File("helloworld.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int i;
while ((i = fis.read()) != -1) {
System.out.print((char) i);
}
} catch (IOException e) {
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream:字节流,节点流,输出流
@Test
public void testFileOutputStream() {
File file = new File("hello.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte b[] = "i love china i love the world!".getBytes();
fos.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
FileReader、FileWriter
/**
* 使用字符流复制文件
* 文本使用
* @param src源文件路径
* @param des目标文件路径
*/
public void copyFileByReaderWriter(String src, String des) {
File inFile = new File(src);
File outFile = new File(des);
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(inFile);
fw = new FileWriter(outFile);
char[] b = new char[1024];
int len;
while ((len = fr.read(b)) != -1) {
fw.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
FileInputStream、FileOutputStream
/**
* 使用节点流实现复制文件
* 非文本使用
* @param src源文件路径
* @param des目标文件路径
*/
public void copyFileByInputOutput(String src, String des) {
long start=System.currentTimeMillis();
File inFile = new File(src);
File outFile = new File(des);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.out.println("FileInoputOutputStream spend time=="+(end-start));
}
-----------------------------------------------------------------------------------------------------------------------------------------------
BufferedInputStream、BufferedOutputStream
/***
* 使用bufferedInputOutputStream实现复制
* 适用非文本操作,效率比FileInputStream、FileOutputStream要快
* @param src
* @param des
*/
public void copyFileByBufferedInputStreamOutputStream(String src,String des) {
long start=System.currentTimeMillis();
File inFile = new File(src);
File outFile = new File(des);
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bis!=null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
long end=System.currentTimeMillis();
System.out.println("BufferedInputOutputStream spend time=="+(end-start));
}
BufferedReader、BufferedWriter
/**
* 使用BufferedReader、BufferedWriter实现文件的读写
* @param src
* @param des
*/
public void copyFileByBufferedReaderWriter(String src,String des) {
long start=System.currentTimeMillis();
File inFile = new File(src);
File outFile = new File(des);
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader(inFile);
fw = new FileWriter(outFile);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
char[] b = new char[1024];
int len;
while ((len = br.read(b)) != -1) {
bw.write(b, 0, len);
}
} catch (Exception e) {
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
if(br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end=System.currentTimeMillis();
System.out.println("BufferedReaderWriter spend time=="+(end-start));
}
通过测试BufferedInputStream、BufferedOutputStream操作非文本效率最高
/**
* 测试视频复制
*/
@Test
public void testCopyFileToVoide() {
String src = "E:\\123.wmv";
String f_des = "E:\\f_v456.wmv";
String b_des="E:\\b_v456.wmv";
String brw_des="E:\\brw_v456.wmv";
copyFileByInputOutput(src, f_des);//FileInoputOutputStream spend time==1221
copyFileByBufferedInputStreamOutputStream(src, b_des);//BufferedInputOutputStream spend time==267
copyFileByBufferedReaderWriter(src, brw_des);//copyFileByBufferedReaderWriter spend time==3750
}