IO 复习字节流字符流拷贝文件

Posted 拂髯客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
	}
}

  

以上是关于IO 复习字节流字符流拷贝文件的主要内容,如果未能解决你的问题,请参考以下文章

第2章 字符流与字节流

IO流 文件夹的拷贝(字节流)

JavaSE——IO流(字节流&字符流)

字节流与字符流复习

java IO流文件拷贝文件(字节流标准写法)

Java之IO初识(字节流和字符流)