1.字符流对文件的copy
注意:字符流只能对文本文件进行操作,字符类最好用字符流去copy。
package com.yidongxueyuan.Iofile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TextCopyDemo_01 {
public static void main(String[] args) throws IOException {
/**
* 字符流方法
* 1.单个字符copy
* 2.字符数组copy
* 3.高效单字符copy
* 4.高效字符数组copy
* 5.高效字符行copy
*/
copy_01();
copy_02();
copy_03();
copy_04();
copy_05();
}
private static void copy_01() throws IOException
{
FileReader fr = new FileReader("D:\\dn.txt");
FileWriter fw = new FileWriter("E:\\CopyFile\\copy_01.txt");
int num = 0;
long start = System.currentTimeMillis();
while((num=fr.read())!=-1)
{
fw.write(num);
fw.flush();
}
long end = System.currentTimeMillis();
System.out.println("time_01: "+(end-start));
fr.close();
fw.close();
}
private static void copy_02() throws IOException
{
FileReader fr = new FileReader("D:\\dn.txt");
FileWriter fw = new FileWriter("E:\\CopyFile\\copy_02.txt");
int len = 0;
char ch[] = new char[1024];
long start = System.currentTimeMillis();
while((len=fr.read(ch))!=-1)//len是读取的字符数。如果已到达流的末尾,则返回 -1 。
{
// fw.write(ch,0,len);
// fw.write(len);
// fw.write(ch.toString(),0,len);//toString()方法没有被重写
// fw.write(new String(ch),0,len);
fw.write(new String(ch,0,len));//传递一个字符串对象!
fw.flush();
}
long end = System.currentTimeMillis();
System.out.println("time_02: "+(end-start));
fr.close();
fw.close();
}
private static void copy_03() throws IOException
{
BufferedReader bur = new BufferedReader(new FileReader("D:\\dn.txt"));
BufferedWriter buw = new BufferedWriter(new FileWriter("E:\\CopyFile\\copy_03.txt"));
int num = 0;
long start = System.currentTimeMillis();
while((num=bur.read())!=-1)
{
buw.write(num);
buw.flush();
}
long end = System.currentTimeMillis();
System.out.println("time_03: "+(end-start));
bur.close();
buw.close();
}
private static void copy_04() throws IOException
{
BufferedReader bur = new BufferedReader(new FileReader("D:\\dn.txt"));
BufferedWriter buw = new BufferedWriter(new FileWriter("E:\\CopyFile\\copy_04.txt"));
int num = 0;
char ch[] = new char[1024];
long start = System.currentTimeMillis();
while((num=bur.read(ch,0,ch.length))!=-1)//num为读取的字符数,如果已到达流末尾,则返回 -1
{
// buw.write(ch,0,num);
buw.write(new String(ch),0,num);
buw.flush();
}
long end = System.currentTimeMillis();
System.out.println("time_04: "+(end-start));
bur.close();
buw.close();
}
private static void copy_05() throws IOException
{
BufferedReader bur = new BufferedReader(new FileReader("D:\\dn.txt"));
BufferedWriter buw = new BufferedWriter(new FileWriter("E:\\CopyFile\\copy_05.txt"));
String str = null;
long start = System.currentTimeMillis();
while((str=bur.readLine())!=null)//str为包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null(遇到换行就停)
{
// buw.write(ch,0,num);
buw.write(str);//写完一行,使用的是父类中的write(String str);方法
buw.newLine();
buw.flush();
}
long end = System.currentTimeMillis();
System.out.println("time_05: "+(end-start));
bur.close();
buw.close();
}
}
运行结果如下:
经检查,copy出来的文件与源文件属性相同。
由此可见,虽然都是对文件进行了copy,但是快慢却不相同, 单个字符copy>高效单字符copy>字符数组copy>高效字符行copy>高效字符数组copy。由此得出,字符流copy文本文件时,使用高效字符数组copy文本文件是较合适的选择。
2.字节流对文件的copy
package com.yidongxueyuan.Iofile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class StreamCopyDemo_01 {
public static void main(String[] args) throws IOException, InterruptedException {
/**
* 字节流copy
* 一、文本文档
* 1.单个字节copy
* 2.字节数组copy
* 3.高效的单字节copy
* 4.高效的字节数组copy
* 二、图片
* 1.单个字节copy
* 2.字节数组copy
* 3.高效的单字节copy
* 4.高效的字节数组copy
* 三、视频
* 1.单个字节copy
* 2.字节数组copy
* 3.高效的单字节copy
* 4.高效的字节数组copy
*/
txtcopy_01();
txtcopy_02();
txtcopy_03();
txtcopy_04();
photocopy_01();
photocopy_02();
photocopy_03();
photocopy_04();
videocopy_01();
videocopy_02();
videocopy_03();
videocopy_04();
}
//文本文档单个字节copy
private static void txtcopy_01() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:\\dn.txt");
FileOutputStream fos = new FileOutputStream("E:\\CopyFile\\1copy_01.txt");
int num = 0;
long start = System.currentTimeMillis();
while((num=fis.read())!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
fos.write(num);
}
long end = System.currentTimeMillis();
System.out.println("txttime_01: "+(end-start));
fis.close();
fos.close();
}
//文本文档字节数组copy
private static void txtcopy_02() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:\\dn.txt");
FileOutputStream fos = new FileOutputStream("E:\\CopyFile\\1copy_02.txt");
int len = 0;
byte b[] = new byte[1024];
long start = System.currentTimeMillis();
while((len=fis.read(b))!=-1)//len为读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。
{
// fos.write(b);
fos.write(b, 0, len);//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
}
long end = System.currentTimeMillis();
System.out.println("txttime_02: "+(end-start));
fis.close();
fos.close();
}
//文本文档高效的单字节copy
private static void txtcopy_03() throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\dn.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\CopyFile\\1copy_03.txt"));
int num = 0;
long start = System.currentTimeMillis();
while((num=bis.read())!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
bos.write(num);
}
long end = System.currentTimeMillis();
System.out.println("txttime_03: "+(end-start));
bis.close();
bos.close();
}
//文本文档高效的字节数组copy
private static void txtcopy_04() throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\dn.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\CopyFile\\1copy_04.txt"));
int len = 0;
byte b[] = new byte[1024];
long start = System.currentTimeMillis();
while((len=bis.read(b,0,b.length))!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
// bos.write(num);
bos.write(b,0,len);//这里必须有范围,否则重复
}
long end = System.currentTimeMillis();
System.out.println("txttime_04: "+(end-start));
bis.close();
bos.close();
}
//图片单个字节copy
private static void photocopy_01() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:\\dn.jpg");
FileOutputStream fos = new FileOutputStream("E:\\CopyFile\\2copy_01.jpg");
int num = 0;
long start = System.currentTimeMillis();
while((num=fis.read())!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
fos.write(num);
}
long end = System.currentTimeMillis();
System.out.println("phototime_01: "+(end-start));
fis.close();
fos.close();
}
//图片字节数组copy
private static void photocopy_02() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:\\dn.jpg");
FileOutputStream fos = new FileOutputStream("E:\\CopyFile\\2copy_02.jpg");
int len = 0;
byte b[] = new byte[1024];
long start = System.currentTimeMillis();
while((len=fis.read(b))!=-1)//len为读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。
{
// fos.write(b);
fos.write(b, 0, len);//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
}
long end = System.currentTimeMillis();
System.out.println("phototime_02: "+(end-start));
fis.close();
fos.close();
}
//图片高效的单字节copy
private static void photocopy_03() throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\dn.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\CopyFile\\2copy_03.jpg"));
int num = 0;
long start = System.currentTimeMillis();
while((num=bis.read())!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
bos.write(num);
}
long end = System.currentTimeMillis();
System.out.println("phototime_03: "+(end-start));
bis.close();
bos.close();
}
//图片高效的字节数组copy
private static void photocopy_04() throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\dn.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\CopyFile\\2copy_04.jpg"));
int len = 0;
byte b[] = new byte[1024];
long start = System.currentTimeMillis();
while((len=bis.read(b,0,b.length))!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
// bos.write(num);
bos.write(b,0,len);//这里必须有范围,否则重复
}
long end = System.currentTimeMillis();
System.out.println("phototime_04: "+(end-start));
bis.close();
bos.close();
}
//视频单个字节copy
private static void videocopy_01() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:\\(二)PrintStream类.wmv");
FileOutputStream fos = new FileOutputStream("E:\\CopyFile\\3copy_01.wmv");
int num = 0;
long start = System.currentTimeMillis();
while((num=fis.read())!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
fos.write(num);
}
long end = System.currentTimeMillis();
System.out.println("videotime_01: "+(end-start));
fis.close();
fos.close();
}
//视频字节数组copy
private static void videocopy_02() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:\\(二)PrintStream类.wmv");
FileOutputStream fos = new FileOutputStream("E:\\CopyFile\\3copy_02.wmv");
int len = 0;
byte b[] = new byte[1024];
long start = System.currentTimeMillis();
while((len=fis.read(b))!=-1)//len为读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。
{
// fos.write(b);
fos.write(b, 0, len);//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
}
long end = System.currentTimeMillis();
System.out.println("videotime_02: "+(end-start));
fis.close();
fos.close();
}
//视频高效的单字节copy
private static void videocopy_03() throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\(二)PrintStream类.wmv"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\CopyFile\\3copy_03.wmv"));
int num = 0;
long start = System.currentTimeMillis();
while((num=bis.read())!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
bos.write(num);
}
long end = System.currentTimeMillis();
System.out.println("videotime_03: "+(end-start));
bis.close();
bos.close();
}
//视频高效的字节数组copy
private static void videocopy_04() throws IOException
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\(二)PrintStream类.wmv"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\CopyFile\\3copy_04.wmv"));
int len = 0;
byte b[] = new byte[1024];
long start = System.currentTimeMillis();
while((len=bis.read(b,0,b.length))!=-1)//num为下一个数据字节;如果到达流的末尾,则返回 -1。
{
// bos.write(num);
bos.write(b,0,len);//这里必须有范围,否则重复
}
long end = System.currentTimeMillis();
System.out.println("videotime_04: "+(end-start));
bis.close();
bos.close();
}
}
运行结果如下:
经检查,copy过来的文件属性值与源文件相同,copy成功。
分析得知: 单个字节copy>高效的单字节copy>字节数组copy>高效的字节数组copy。显然,高效字节数组copy方法是较合适的。
总结:copy文件时,如果是字符类,则可以在条件允许的情况下,使用高效字符数组copy。如果不是,则可以选择高效字节数组copy。
3.跟踪行号的缓冲字符输入流
read()读取单个字符。行结束符被压缩为一个换行('\n')字符。无论何时读取行结束符,当前行号将加 1。
private static void copy_05() throws IOException,FileNotFoundException
{
//跟踪行号的缓冲字符输入流
LineNumberReader lnr = new LineNumberReader(new FileReader("D:\\dn.txt"));
BufferedWriter buw = new BufferedWriter(new FileWriter("E:\\CopyFile\\tcopy5.docx"));
int num = 0;
long start1 = System.currentTimeMillis();
while((num=lnr.read())!=-1)//读到行结束符的时候行结束符被压缩为一个换行('\n')字符,因此在记事本中不能正常显示。
{
buw.write(num);
buw.flush();
}
long end1 = System.currentTimeMillis();
System.out.println("copytime5: "+(end1-start1));
lnr.close();
buw.close();
}
copy之前文件里的数据:
copy之后文件里的数据:
可以看到,由于行分隔符被压缩为“\n”,而记事本的行分隔符为“/r/n”,因此不能正常显示。