标签:
简单的使用IO流是很简单的~
package com.j2se.d59.demo;
import java.io.*;
//IO里的对象来自于java.io.*;
//IO流的输入输出是相对于内存RAM的
//分类:单位【字节流、字符流】or方向【输入流、输出流】or功能【节点流、处理流】
//所有的IO流由四个抽象类扩展而来:
//InputStream 的子类 FileInputStream BufferedInputStream
//OutputStream 的子类FileInputStream BufferedOutputStream
//Reader 的子类FileReader BufferedReader
//Writer 的子类FileWriter BufferedWriter
public class IOTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 以下代码用字节流和字符流2种方法完成文件复制功能
// 1 准备
String path1 = "D:\\Java\\Test\\红楼梦-曹雪芹.txt";
String path2 = "D:\\Java\\Test\\程序创建:红楼梦-曹雪芹.txt";
String path3 = "D:\\Java\\Test\\红楼梦-曹雪芹 - 副本.txt";
String path4 = "D:\\Java\\Test\\程序创建:红楼梦-曹雪芹 - 副本.txt";
byte[] b = new byte[1024 * 256];
char[] c = new char[1024 * 256];
// 2 声明
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
// 3 创建
try {
fis = new FileInputStream(path1);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(path2);
bos = new BufferedOutputStream(fos);
fr = new FileReader(path3);
br = new BufferedReader(fr);
fw = new FileWriter(path4);
bw = new BufferedWriter(fw);
// 4读取
int ib = bis.read(b);
// 5判断-方法1
long t1 = System.currentTimeMillis();
while (ib > 0) {
// 6复制
bos.write(b, 0, ib);
// 7循环
ib = bis.read(b);
}
// 8刷新
bw.flush();
long t2 = System.currentTimeMillis();
System.out.println("使用字节流复制文件完成,用时:" + (t2 - t1) + "毫秒");
long t3 = System.currentTimeMillis();
int ic = br.read(c);
// 判断-方法2
while (ic > 0) {
// 复制
bw.write(c, 0, ic);
// 循环
ic = br.read(c);
}
// 刷新
bw.flush();
long t4 = System.currentTimeMillis();
System.out.println("使用字符流复制文件完成,用时:" + (t4 - t3) + "毫秒");
// =============以下全是异常处理代码段,可以不看=============
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 9 关闭
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/qixiawentang/p/5475493.html