字节流的读写(copy)/字符流的读写(copy)?

	//字节流的缓冲流的使用  读写功能
	@Test
	public void testcopy(){
		long start=System.currentTimeMillis();
		String src="C:\\Users\\Administrator\\Desktop\\hetao.jpg";
		String dest="C:\\Users\\Administrator\\Desktop\\hetao4.jpg";
		copyFile(src,dest);
		long end=System.currentTimeMillis();
		System.out.println((end-start));
		
	}

	private void copyFile(String src, String dest) {
		// TODO Auto-generated method stub
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		
		try{
			//1.提供读入,写出的文件
			File file1=new File(src);
			File file2=new File(dest);
			//2.创建相应的节点
			FileInputStream fis=new FileInputStream(file1);
			FileOutputStream fos=new FileOutputStream(file2);
			//3.缓冲流的对象
			bis=new BufferedInputStream(fis);
			bos=new BufferedOutputStream(fos);
			//4.具体的实现
			byte[] b=new byte[1024];
			int len;
			while((len=bis.read(b))!=-1){
				bos.write(b, 0, len);
				bos.flush();//刷新一下
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(bos!=null){
				try {
					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();
				}
			}
		}
	}
//字符流的缓冲流的使用  读写功能
	
	@Test
	public void test3(){
		
	    BufferedReader br=null;
	    BufferedWriter bw=null;
	    try{
	    	//
	    	File file=new File("C:\\Users\\Administrator\\Desktop\\1.docx");
	    	File file1=new File("C:\\Users\\Administrator\\Desktop\\6.docx");
	    	//
	    	FileReader  fr=new FileReader(file);
	    	FileWriter  fw=new FileWriter(file1);
	    	//字符流的缓冲流的对象就有了
	    	br=new BufferedReader(fr);
	    	bw=new BufferedWriter(fw);
	    	String str;
	    	//一读读一行
	    	while((str=br.readLine())!=null){
	    		//System.out.println(str);
	    		bw.write(str);
	    		bw.newLine();
	    		bw.flush();
	    	}
	    	
	    	 
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	if(bw!=null){
	    		try {
					bw.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();
				}
	    	}
	    }
	}
	

猜你喜欢

转载自blog.csdn.net/Java_stud/article/details/82320378