1. 程式人生 > >java中複製文字檔案的方式我總結為14種(按字元讀取4中,按位元組讀取8種!??)

java中複製文字檔案的方式我總結為14種(按字元讀取4中,按位元組讀取8種!??)

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種方式(位元組流讀取方式),不能用字元流讀取