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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

利用數(shù)據(jù)流實(shí)現(xiàn)復(fù)制文件

2019-11-11 01:13:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
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 { /** * 實(shí)現(xiàn)復(fù)制文件 運(yùn)用FileInputStream,FileOutputStream對(duì)文件進(jìn)行讀取寫(xiě)入 只能一個(gè)一個(gè)字符復(fù)制(速度慢) */ public static void copyFile(String sourceName, String dest) { // 1:讀取一個(gè)文件 // 2:寫(xiě)入一個(gè)文件 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 * 實(shí)現(xiàn)復(fù)制文件(提速) 運(yùn)用FileInputStream,FileOutputStream對(duì)文件進(jìn)行讀取寫(xiě)入 引用數(shù)組byte[] 缺點(diǎn): * 當(dāng)使用數(shù)組的時(shí)候,如果文件里的字符的長(zhǎng)度不是數(shù)組的倍數(shù)的時(shí)候,拿到的數(shù)據(jù)會(huì)重復(fù) */ public static void test2() { byte[] myArray = new byte[3]; try { InputStream fis = new FileInputStream("src/a.txx"); while (fis.read(myArray) != -1) { // 如果達(dá)到文件尾,跳出循環(huán) 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 * 實(shí)現(xiàn)復(fù)制文件(提速)(改進(jìn)) 運(yùn)用FileInputStream,FileOutputStream對(duì)文件進(jìn)行讀取寫(xiě)入 引用數(shù)組byte[] 缺點(diǎn): * 當(dāng)文件的內(nèi)容是中的時(shí)候會(huì)出現(xiàn)亂碼 */ public static void test3() { byte[] myArray = new byte[3]; int len;// 長(zhǎng)度 try { FileInputStream is = new FileInputStream("src/a.txt"); while ((len = is.read(myArray)) != -1) { // 定義一個(gè)新的數(shù)組,防止有多余的數(shù)據(jù) 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 * 實(shí)現(xiàn)復(fù)制文件 (速度快) 運(yùn)用FileInputStream,FileOutputStream對(duì)文件進(jìn)行讀取寫(xiě)入 只能一個(gè)一個(gè)字符復(fù)制(速度快) * 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"); // 如果到達(dá)文件尾,無(wú)法滿足while的條件,不會(huì)執(zhí)行while語(yǔ)句的內(nèi)容 while ((len = is.read(myArray)) != -1) { // 定義一個(gè)新的數(shù)組,防止有多余的數(shù)據(jù) 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 * 實(shí)現(xiàn)復(fù)制文件 (速度快)(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系統(tǒng)是已經(jīng)直接封裝好的,可以直接用,速度會(huì)更快一些 os.write(數(shù)字長(zhǎng)度,起始,末尾) */ 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("執(zhí)行成功"); } /** * test6 * 使用緩沖區(qū),提高速率 * 用到FileInputStream,FileOutputStream,BufferedInputStream,BufferedOutputStream * 用緩沖區(qū)的好處 * 沒(méi)有緩存區(qū),那么每read一次,就會(huì)發(fā)送一次IO操作;有緩存區(qū),第一次read時(shí),會(huì)一下讀取x個(gè)字節(jié)放入緩存區(qū),然后后續(xù)的read都會(huì)從緩存中讀取,當(dāng)read到緩存區(qū)末尾時(shí),會(huì)再次讀取x個(gè)字節(jié)放入緩存區(qū)。很明顯,第二種方式,會(huì)減少I(mǎi)O操作,效率更高,缺點(diǎn)就是,內(nèi)存占用的多。 */ 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 * 當(dāng)我們沒(méi)有文件的時(shí)候,如何使用流 * 這里用到的是字節(jié)數(shù)組輸出流和字節(jié)數(shù)組輸入流 * ByteArrayOutputStream類(lèi)是在創(chuàng)建它的實(shí)例時(shí),程序內(nèi)部創(chuàng)建一個(gè)byte型別數(shù)組的緩沖區(qū),然后利用ByteArrayOutputStream和ByteArrayInputStream的實(shí)例向數(shù)組中寫(xiě)入或讀出byte型數(shù)據(jù)。在網(wǎng)絡(luò)傳輸中我們往往要傳輸很多變量,我們可以利用ByteArrayOutputStream把所有的變量收集到一起,然后一次性把數(shù)據(jù)發(fā)送出去。 */ 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 * 問(wèn)題:當(dāng)有中文的時(shí)候,可能會(huì)出現(xiàn)亂碼 */ 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 * 問(wèn)題:當(dāng)有中文的時(shí)候,可能出現(xiàn)亂碼,當(dāng)有了流的時(shí)候 * 出現(xiàn)亂碼的原因是:字符的是字節(jié)是2個(gè)長(zhǎng)度,而read(byte[])以一個(gè)字節(jié)掃描。將字符的兩個(gè)字節(jié)拆分開(kāi)了,所以出現(xiàn)亂碼 */ 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 * 解決方法:利用數(shù)組Array[2],和ByteArrayInputeStream,ByteArrayOutputStream,將字符的數(shù)據(jù)存儲(chǔ)在一起(以二的倍數(shù)存儲(chǔ))最后一起輸出就可以避免出現(xiàn)亂碼 */ 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 * 引用字符流解決中文問(wèn)題 Reader 抽象類(lèi),InputStreamReader實(shí)現(xiàn)類(lèi) * 只能輸出一個(gè)中文(一個(gè)字符) */ 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 * 引用字符流解決中文的問(wèn)題 Reader抽象類(lèi),InputStreamReader實(shí)現(xiàn)類(lèi) 讀取到的數(shù)組中,一次可以讀取多個(gè)字符 * */ 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 * 用字符流寫(xiě)入數(shù)據(jù)到文件里 * 這里用到Writer,OutputStreamWriter,F(xiàn)ileOutputStream */ public static void test15() { try { Writer writer = new OutputStreamWriter(new FileOutputStream("src/a.txt", true)); //指向操作對(duì)象 //true表示可以在文件的原有內(nèi)用上寫(xiě)入內(nèi)容 writer.write("jiajia"); writer.flush(); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("寫(xiě)入成功"); } /** * test16 * 復(fù)制文件 * 利用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;//長(zhǎng)度 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("復(fù)制成功"); } 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(); } } /** * 利用緩沖,使寫(xiě)入字符流更高效 * 用到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 * 為了能夠?qū)懭霐?shù)據(jù)的時(shí)候更加方便,我們要引入printWriter * 用到PrintWriter */ public static void test18() { try { PrintWriter pw = new PrintWriter("src/e.txt"); pw.print("回車(chē)/n"); pw.print("新行/r"); pw.print(18); pw.flush(); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("執(zhí)行成功"); } /** * 讀取文件的時(shí)候更加簡(jiǎn)單 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(); } } /** * 復(fù)制 利用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("執(zhí)行成功"); } /** * 復(fù)制 利用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()); } //將緩存區(qū)的數(shù)據(jù)強(qiáng)制寫(xiě)入到目標(biāo)文件 bfw.flush(); bfw.close(); sc.close(); System.out.println("執(zhí)行成功"); } 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>執(zhí)行耗時(shí) : " + (System.currentTimeMillis() - a) / 1000f + " 秒 "); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪雅县| 喀喇沁旗| 青岛市| 古田县| 天等县| 建湖县| 孟津县| 陆河县| 陵川县| 扎鲁特旗| 尉氏县| 桃源县| 岳普湖县| 延吉市| 镇原县| 鹿泉市| 满洲里市| 江西省| 宁安市| 大安市| 海晏县| 贺兰县| 西昌市| 祁东县| 安图县| 清新县| 香河县| 龙陵县| 望奎县| 商水县| 湘西| 千阳县| 周口市| 中山市| 绵竹市| 西畴县| 肥西县| 邢台市| 沈丘县| 曲靖市| 灵璧县|