码迷,mamicode.com
首页 > 编程语言 > 详细

java中复制文本文件的方式我总结为14种(按字符读取4中,按字节读取8种!??)

时间:2015-07-26 17:30:41      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

java中复制文件的方式

如果按照字符来读取的话,可以有4种,基本的2种,高效的2种,高效特殊的1种

第0种:

public class CopyFileDemo {
	public static void main(String[] args) throws Exception{
		//封裝数据源
		BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
		//封装目的地
		BufferedWriter writer = new BufferedWriter(new FileWriter("b.txt"));
		
		//读写数据
		String line = null;
		while((line=reader.readLine())!=null){
			writer.write(line);
			writer.newLine();//添加新行,不然写到文件里面的内容就没有换行
			writer.flush();
		}
		//释放资源
		writer.close();
		writer.close();
	}
}


第一种:加入缓冲区Buffered一次读取一个字符数组

public class CopyFileTest {
	public static void main(String[] args) throws Exception{
		//封装数据源
		BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
		//封装目的地
		BufferedWriter writer = new BufferedWriter(new FileWriter("b.txt"));
		
		//两种方式其中一种一次读取一个字符数组
		char[] chs = new char[1024];
		int len=0;
		while((len=reader.read(chs))!=-1){
			writer.write(chs,0,len);
			writer.flush();//记得刷新
		}
		//释放资源
		writer.close();
		reader.close();
	}
}

第二种:加入缓冲区Buffered一次读取一个字符

public class CopyFileTest {
	public static void main(String[] args) throws Exception{
		
		//封装数据源
		BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
		//封装目的地
		BufferedWriter writer = new BufferedWriter(new FileWriter("b.txt"));
		//一次读取一个字符
		int ch=0;
		while((ch=reader.read())!=-1){
			writer.write(ch);
		}
		//释放资源
		writer.close();
		reader.close();
		
	}
}

第三种:普通的一次读取一个字符数组

public class BufferedCopyFileTest {
	public static void main(String[] args) throws Exception{
		
		//封装数据源
		FileReader reader = new FileReader("a.txt");
		//封装目的地
		FileWriter writer = new FileWriter("b.txt");
		//一次读取一个字符数组
		char[] chs = new char[1024];
		int len=0;
		while((len=reader.read(chs))!=-1){
			writer.write(chs,0,len);
			writer.flush();
		}
		writer.close();
		reader.close();
	}
}

第三种:普通的一次读取一个字符

public class BufferedCopyFileTest {
	public static void main(String[] args) throws Exception{
		//封装数据源
		FileReader reader = new FileReader("a.txt");
		//封装目的地
		FileWriter writer = new FileWriter("b.txt");
		//一次读取一个字符
		int ch=0;
		while((ch=reader.read())!=-1){
			writer.write(ch);
		}
		writer.close();
		reader.close();
		
	}
}

如果按照字节来读取的话,可以有8中

第一种:把字节流转换为字符流来一次读取一个字符

public class Copy {
	public static void main(String[] args) throws Exception {
		//封装数据源(把字节流转换为字符流)
		InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
		//封装目的地(把字节流转换为字符流)
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"));
		//一次读取一个字符
		int ch=0;
		while((ch=isr.read())!=-1){
			osw.write(ch);
		}
		//关闭资源
		osw.close();
		isr.close();
	}
}

第二种:把字节流转换为字符流来一次读取一个字符数组

public class Copy {
	public static void main(String[] args) throws Exception {
		//封装数据源(把字节流转换为字符流)
		InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
		//封装目的地(把字节流转换为字符流)
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"));
		//一次读取一个字符数组
		char[] chs = new char[1024];
		int len=0;
		while((len=isr.read(chs))!=-1){
			osw.write(chs,0,len);
			osw.flush();
		}
		//关闭资源
		osw.close();
		isr.close();
	}
}
  第三种:高效字节流一次读写一个字节数组:

 public static void method4(String srcString, String destString)  
                throws IOException {  
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(  
                    srcString));  
            BufferedOutputStream bos = new BufferedOutputStream(  
                    new FileOutputStream(destString));  
      
            byte[] bys = new byte[1024];  
            int len = 0;  
            while ((len = bis.read(bys)) != -1) {  
                bos.write(bys, 0, len);  
            }  
      
            bos.close();  
            bis.close();  
        }  

第四种:高效字节流一次读取一个字符:

 public static void method3(String srcString, String destString)  
                throws IOException {  
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(  
                    srcString));  
            BufferedOutputStream bos = new BufferedOutputStream(  
                    new FileOutputStream(destString));  
      
            int by = 0;  
            while ((by = bis.read()) != -1) {  
                bos.write(by);  
      
            }  
      
            bos.close();  
            bis.close();  
        }  

第五种:普通的字节流对象一次读取一个字节数组

public static void method2(String srcString, String destString)  
                throws IOException {  
            FileInputStream fis = new FileInputStream(srcString);  
            FileOutputStream fos = new FileOutputStream(destString);  
      
            byte[] bys = new byte[1024];  
            int len = 0;  
            while ((len = fis.read(bys)) != -1) {  
                fos.write(bys, 0, len);  
            }  
      
            fos.close();  
            fis.close();  
        }  
      

第六种:普通的字节流对象一次读取一个字节

public static void method1(String srcString, String destString)  
                throws IOException {  
            FileInputStream fis = new FileInputStream(srcString);  
            FileOutputStream fos = new FileOutputStream(destString);  
      
            int by = 0;  
            while ((by = fis.read()) != -1) {  
                fos.write(by);  
            }  
      
            fos.close();  
            fis.close();  
        }  
第七种:BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b,txt")));然后一次读取一个字符

第八种:BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b,txt")));然后一次读取一个字符数组


注意复制图片、视频、音频的话,只有4种方式(字节流读取方式),不能用字符流读取

版权声明:本文为博主原创文章,未经博主允许不得转载。

java中复制文本文件的方式我总结为14种(按字符读取4中,按字节读取8种!??)

标签:

原文地址:http://blog.csdn.net/u012110719/article/details/47068007

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!