zoukankan      html  css  js  c++  java
  • IO 复习字节流字符流拷贝文件

    /* 本地文件 URL 文件拷贝 */
    /*文本文件拷贝 可以通过 字符流,也可以通过字节流*/
    /*二进制文件拷贝 只可以通过字节流*/
    /* 希望这个例子能帮助搞懂 字符流与字节流的区别 */
    import java.io.*; //in order to utilize stream object
    import java.util.*; // in order to utilize ArrayList
    import java.net.*;
    class Copy{
    	public static void main(String [] args){
    		boolean flag=false;
    		String src="C:\Users\Ghc\Desktop\extractzip.bat";
    		String dest="C:\Users\Ghc\Desktop\Test\extractzip_copy.bat";
    		System.out.println("copy textDoc: from "+src+" to "+dest+(copyTextDoc(src,dest) ? "Successfully!":"Failed"));
    		
    		
    		src="C:\Users\Ghc\Desktop\psb.jpg";
    		dest="C:\Users\Ghc\Desktop\Test\psb.jpg";
    		System.out.println("copy textDoc: from "+src+" to "+dest+(copyBinFile(src,dest) ? "Successfully!":"Failed"));
    		
    		
    		String url="http://qiniuuwmp3.changba.com/852316795.mp3";
    		String destPath="C:\Users\Ghc\Desktop\Test\music.mp3";
    		
    		// downLoad mp3 from URL
    		downLoadMP3FromURL(url,destPath);
    	}
    	
    	public static void downLoadMP3FromURL(String url,String destPath){
    		InputStream urlInpts=null;
    		BufferedInputStream bufinpts=null;
    		BufferedOutputStream bufoutpts=null;
    		try{
    			urlInpts=new URL(url).openStream();
    			bufinpts=new BufferedInputStream(urlInpts);
    			bufoutpts=new BufferedOutputStream(new FileOutputStream(destPath));
    			
    			byte[] musicBuf=new byte[8192];
    			int len=-1;
    			while((len=bufinpts.read(musicBuf))!=-1){
    					bufoutpts.write(musicBuf,0,len);
    					bufoutpts.flush();
    				}
    			}
    			catch(MalformedURLException mfurle){
    				mfurle.printStackTrace();
    			}
    			catch(IOException ioe){
    				ioe.printStackTrace();
    			}
      }
    	
    	public static boolean copyTextDoc(String src,String dest){
    		boolean flag=true;
    		
    		BufferedReader bufr=null;
    		
    		BufferedWriter bufw=null;
    		ArrayList<String> lineList=null;
    		
    		
    		try{
    			//read text file
    			
    			//bufr=new BufferedReader(new InputStreamReader(new FileInputStream(src)));
    			//等价于
    			bufr=new BufferedReader(new FileReader(src));
    			
    			//bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)));
    			//等价于
    			bufw=new BufferedWriter(new FileWriter(dest));
    			lineList=new ArrayList<String>();
    			
    			String line=null;
    		
    		while((line=bufr.readLine())!=null){
    			lineList.add(line);
    			bufw.write(line,0,line.length());
    			bufw.newLine();
    			bufw.flush(); // must to flush!!! attention!!!
    			}
    		
    		Iterator<String> it=lineList.iterator();
    		while(it.hasNext()){
    			line=it.next();
    			System.out.println(line+" from iterator");
    			}
    		}
    		catch(IOException ioe){
    			ioe.printStackTrace();
    			flag=false;
    			
    			//write text file
    			
    			
    		} 
    		finally{
    			if(bufr!=null)
    				try{
    					bufr.close();
    					bufr=null;
    				}
    				catch(IOException ioe){
    					ioe.printStackTrace();
    				}
    			if(bufw!=null)
    				try{
    					bufw.close();
    					bufw=null;					
    				}
    				catch(IOException ioe){
    					ioe.printStackTrace();
    				}
    		}
    		return flag;
    	}
    	public static boolean copyBinFile(String src,String dest){
    		boolean flag=true;
    		BufferedInputStream bufinpts=null;
    		
    		BufferedOutputStream bufoutpts=null;
    		byte [] buf=new byte[1024];
    		int len=-1;
    		try{
    			bufinpts=new BufferedInputStream(new FileInputStream(src));
    			bufoutpts=new BufferedOutputStream(new FileOutputStream(dest));
    			
    			while((len=bufinpts.read(buf))!=-1){
    					bufoutpts.write(buf,0,len); // not need to flush, but the last buf may not been copied
    					// print binary data see see , it's funny!!!
    					/* for(int i=0;i<len;i++){
    						System.out.print(buf[i]);
    					} */
    					bufoutpts.flush();
    			}
    		}
    		catch(IOException ioe){
    			ioe.printStackTrace();
    			flag=false;
    		}
    		finally{
    			if(bufinpts!=null)
    				try{
    					bufinpts.close();
    					bufinpts=null;
    				}
    			catch(IOException ioe){
    				ioe.printStackTrace();
    			}
    			if(bufoutpts!=null)
    				try{
    					bufoutpts.close();
    					bufoutpts=null;
    				}
    			catch(IOException ioe){
    				ioe.printStackTrace();
    			}
    		}
    		
    		return flag;
    	}
    }
    

      

    如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。
  • 相关阅读:
    Python基础04_str_方法
    Python基础03_pycharm
    Python基础02_基本数据类型_以及while
    Python基础01_介绍_HelloWorld
    Linux基础知识_Shell编程笔记
    python基础之centos6.5 升级 python2.7, 安装pip, MySQLdb
    不得不补:PHP的JSON, SQL
    JS类小功能
    1083.是否存在相等的差(20)
    c++ 的vector sort遇到栈错误
  • 原文地址:https://www.cnblogs.com/Frank99/p/6665122.html
Copyright © 2011-2022 走看看