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

时间:2021-10-15 21:00:53
/* 本地文件 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;
}
}