国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用數據流實現復制文件

2019-11-10 22:08:31
字體:
來源:轉載
供稿:網友
package com.mashensoft;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PRintWriter;import java.io.Reader;import java.io.Writer;import java.util.Scanner;/** * 讀取文本文件,讀取圖片,讀取視頻 * * @author PeicongHe * */public class Homework { /** * 實現復制文件 運用FileInputStream,FileOutputStream對文件進行讀取寫入 只能一個一個字符復制(速度慢) */ public static void copyFile(String sourceName, String dest) { // 1:讀取一個文件 // 2:寫入一個文件 try { FileInputStream fis = new FileInputStream(sourceName);// 源文件地址 FileOutputStream fos = new FileOutputStream(dest);// 目的文件地址 int a = 0; while ((a = fis.read()) != -1) { fos.write(a); } fos.flush(); fos.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test2 * 實現復制文件(提速) 運用FileInputStream,FileOutputStream對文件進行讀取寫入 引用數組byte[] 缺點: * 當使用數組的時候,如果文件里的字符的長度不是數組的倍數的時候,拿到的數據會重復 */ public static void test2() { byte[] myArray = new byte[3]; try { InputStream fis = new FileInputStream("src/a.txx"); while (fis.read(myArray) != -1) { // 如果達到文件尾,跳出循環 for (int i = 0; i < myArray.length; i++) { System.out.print((char) myArray[i]); } } fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test3 * 實現復制文件(提速)(改進) 運用FileInputStream,FileOutputStream對文件進行讀取寫入 引用數組byte[] 缺點: * 當文件的內容是中的時候會出現亂碼 */ public static void test3() { byte[] myArray = new byte[3]; int len;// 長度 try { FileInputStream is = new FileInputStream("src/a.txt"); while ((len = is.read(myArray)) != -1) { // 定義一個新的數組,防止有多余的數據 for (int i = 0; i < len; i++) { System.out.print((char) myArray[i]); } } is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test4 * 實現復制文件 (速度快) 運用FileInputStream,FileOutputStream對文件進行讀取寫入 只能一個一個字符復制(速度快) * write */ public static void test4() { byte[] myArray = new byte[32]; int len; try { InputStream is = new FileInputStream("src/BangBang.flac"); FileOutputStream os = new FileOutputStream("src/BangBangx.flac"); // 如果到達文件尾,無法滿足while的條件,不會執行while語句的內容 while ((len = is.read(myArray)) != -1) { // 定義一個新的數組,防止有多余的數據 byte descArray[] = new byte[len]; for (int i = 0; i < len; i++) { descArray[i] = myArray[i]; } os.write(descArray); } is.close(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test5 * 實現復制文件 (速度快)(jdk) */ public static void test4x(String sourceName, String targetName) { byte myArray[] = new byte[1024]; int len; try { InputStream fis = new FileInputStream(sourceName); FileOutputStream fos = new FileOutputStream(targetName); while ((len = fis.read(myArray)) != -1) { /** * os.write JDK系統是已經直接封裝好的,可以直接用,速度會更快一些 os.write(數字長度,起始,末尾) */ fos.write(myArray, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void test4xx() { try { FileInputStream fis = new FileInputStream("src/a.txt"); FileOutputStream fos = new FileOutputStream("src/axx.txt"); int len; byte[] myArray = new byte[1024]; while ((len = fis.read(myArray)) != -1) { fos.write(myArray, 0, len); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("執行成功"); } /** * test6 * 使用緩沖區,提高速率 * 用到FileInputStream,FileOutputStream,BufferedInputStream,BufferedOutputStream * 用緩沖區的好處 * 沒有緩存區,那么每read一次,就會發送一次IO操作;有緩存區,第一次read時,會一下讀取x個字節放入緩存區,然后后續的read都會從緩存中讀取,當read到緩存區末尾時,會再次讀取x個字節放入緩存區。很明顯,第二種方式,會減少IO操作,效率更高,缺點就是,內存占用的多。 */ public static void test6() { InputStream is; try { is = new FileInputStream("src/BangBang.flac"); FileOutputStream os = new FileOutputStream("src/BangBangxx.flac"); BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(os); byte myArrat[] = new byte[1024]; int len; while ((len = bis.read(myArrat)) != -1) { bos.write(myArrat, 0, len); } bos.flush(); bis.close(); bos.close(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test8 * 當我們沒有文件的時候,如何使用流 * 這里用到的是字節數組輸出流和字節數組輸入流 * ByteArrayOutputStream類是在創建它的實例時,程序內部創建一個byte型別數組的緩沖區,然后利用ByteArrayOutputStream和ByteArrayInputStream的實例向數組中寫入或讀出byte型數據。在網絡傳輸中我們往往要傳輸很多變量,我們可以利用ByteArrayOutputStream把所有的變量收集到一起,然后一次性把數據發送出去。 */ public static void test8() { String content = "hepeicongshidashuaige"; ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes()); int a = 0; byte[] myArray = new byte[3]; try { while ((a = is.read(myArray)) != -1) { for(int i=0;i<a;i++){ System.out.print((char)myArray[i]); } } } catch (IOException e) { e.printStackTrace(); } } /** * test9 * 問題:當有中文的時候,可能會出現亂碼 */ public static void test9() { String content = "你"; byte myArray[] = content.getBytes(); for(int i = 0;i<myArray.length;i++){ System.out.println((char)myArray[i]); } } /** * test10 * 問題:當有中文的時候,可能出現亂碼,當有了流的時候 * 出現亂碼的原因是:字符的是字節是2個長度,而read(byte[])以一個字節掃描。將字符的兩個字節拆分開了,所以出現亂碼 */ public static void test10() { String content = "你"; ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes()); byte a = 0; while ((a = (byte) is.read()) != -1) { System.out.print((char) a); } } /** * test11 * 解決方法:利用數組Array[2],和ByteArrayInputeStream,ByteArrayOutputStream,將字符的數據存儲在一起(以二的倍數存儲)最后一起輸出就可以避免出現亂碼 */ public static void test11() { String content = "你"; ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes()); byte a[] = new byte[2]; try { bis.read(a); System.out.println(new String(a)); } catch (IOException e) { e.printStackTrace(); } } /** * text12 * 同理輸出一串中文 */ public static void test12() { String content = "楊曉怡"; ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes()); byte a[] = new byte[2]; int len; try { while((len=bis.read(a))!=-1){ for(int i=0;i<len-1;i++){ System.out.println(new String(a)); } } } catch (IOException e) { e.printStackTrace(); } } public static void test12x() { try { FileInputStream is = new FileInputStream("src/a.txt"); byte myArray[] = new byte[2]; int a = 0; try { while ((a = is.read(myArray)) != -1) { System.out.println(new String(myArray)); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * test13 * 引用字符流解決中文問題 Reader 抽象類,InputStreamReader實現類 * 只能輸出一個中文(一個字符) */ public static void test13() { try { Reader reader = new InputStreamReader(new FileInputStream("src/a.txt")); int a = reader.read(); System.out.print((char)a); reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test14 * 引用字符流解決中文的問題 Reader抽象類,InputStreamReader實現類 讀取到的數組中,一次可以讀取多個字符 * */ public static void test14() { Reader reader; try { reader = new InputStreamReader(new FileInputStream("src/a.txt")); char myArray[] = new char[3]; int a = 0; while ((a = reader.read(myArray)) != -1) { System.out.println(new String(myArray)); } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test15 * 用字符流寫入數據到文件里 * 這里用到Writer,OutputStreamWriter,FileOutputStream */ public static void test15() { try { Writer writer = new OutputStreamWriter(new FileOutputStream("src/a.txt", true)); //指向操作對象 //true表示可以在文件的原有內用上寫入內容 writer.write("jiajia"); writer.flush(); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("寫入成功"); } /** * test16 * 復制文件 * 利用Reader,Writer,InputStreamReader,OutputStreamReader */ public static void test15x() { try { Reader reader = new InputStreamReader(new FileInputStream("src/a.txt")); Writer writer = new OutputStreamWriter(new FileOutputStream("src/a15x.txt")); char myArray[] = new char[3]; int a = 0;//長度 while ((a = reader.read(myArray)) != -1) { System.out.println(new String(myArray)); writer.write(myArray); } writer.flush(); reader.close(); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("復制成功"); } public static void test15xx(){ try { Reader reader = new InputStreamReader(new FileInputStream("src/a.txt")); Writer writer = new OutputStreamWriter(new FileOutputStream("src/axxx.txt")); char[] myArray=new char[32]; int len; while((len=reader.read(myArray))!=-1){ System.out.println(new String(myArray)); writer.write(myArray); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 利用緩沖,使寫入字符流更高效 * 用到BufferedWriter * BufferedWriter(new OutputStreamWriter(new FileOutputStream(where))) */ public static void test17() { try { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/d.txt"))); bw.write("hello,何沛聰"); bw.flush(); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test18 * 為了能夠寫入數據的時候更加方便,我們要引入printWriter * 用到PrintWriter */ public static void test18() { try { PrintWriter pw = new PrintWriter("src/e.txt"); pw.print("回車/n"); pw.print("新行/r"); pw.print(18); pw.flush(); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("執行成功"); } /** * 讀取文件的時候更加簡單 scanner */ public static void test19() { Scanner sc; try { sc = new Scanner(new FileInputStream("src/a15x.txt")); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * 復制 利用print - sanner */ public static void test19x() { try { Scanner sc=new Scanner(new FileInputStream("src/a.txt")); PrintWriter pw = new PrintWriter("src/acp.txt"); while(sc.hasNextLine()){// System.out.println(sc.nextLine()); pw.print(sc.nextLine()); } sc.close(); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("執行成功"); } /** * 復制 利用BufferedWriter - Scanner */ public static void test19xx(){ try { Scanner sc=new Scanner(new FileInputStream("src/a.txt")); BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/acpx.txt"))); while(sc.hasNextLine()){ bfw.write(sc.nextLine()); } //將緩存區的數據強制寫入到目標文件 bfw.flush(); bfw.close(); sc.close(); System.out.println("執行成功"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { long a = System.currentTimeMillis(); // copyFile("a.txt","b.txt"); // test4(); // test4x("BangBang.flac","BangBangtx.flac"); test19xx(); System.out.println("/r<br>執行耗時 : " + (System.currentTimeMillis() - a) / 1000f + " 秒 "); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深泽县| 维西| 辉县市| 图木舒克市| 突泉县| 南投市| 齐河县| 正镶白旗| 海丰县| 清河县| 磐安县| 汝州市| 丹寨县| 新和县| 锦州市| 翼城县| 油尖旺区| 广西| 阳春市| 中西区| 汕头市| 偏关县| 宜丰县| 博湖县| 乐亭县| 河间市| 南安市| 惠州市| 扶绥县| 元阳县| 贡觉县| 长寿区| 定日县| 临武县| 伊川县| 织金县| 宁武县| 望都县| 瓦房店市| 康定县| 博乐市|