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

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

io流,入門例子代碼

2019-11-11 01:46:48
字體:
供稿:網(wǎng)友
package com.mashensoft.homework;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;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 zongxing * */public class HomeWork {/*** 讀取文本文件,讀取圖片,讀取視頻* @param sourceName 源文件* @param dest 目標(biāo)文件*/public static void copyFile(String sourceName,String dest) {System.out.println("開始copy文件,源文件是:"+sourceName+",目標(biāo)文件是:"+dest);//1:先讀取一個(gè)文件//2:寫入一個(gè)文件try {FileInputStream fis = new FileInputStream(sourceName);FileOutputStream fos = new FileOutputStream(dest);int a = 0;//當(dāng)還未到達(dá)文件尾時(shí),循環(huán)讀取while((a=fis.read())!=-1){//每讀取一個(gè)字節(jié)過來,我們就寫入到另一個(gè)文件里去fos.write(a);//這里只會(huì)正常寫入數(shù)據(jù),不會(huì)把-1寫進(jìn)去}fos.flush();//把緩沖區(qū)里的數(shù)據(jù)強(qiáng)制寫入到文件中fos.close();//關(guān)閉輸出流fis.close();//關(guān)閉輸入流} catch (IOException e) {e.printStackTrace();}}/*** 由于讀取速度太慢,所以要學(xué)習(xí)新的方法,讓我們更快一點(diǎn)*/public static void test2() {byte[] myArray = new byte[3];try {InputStream is = new FileInputStream("a.txt");//如果到達(dá)文件尾,無法滿足while的條件,不會(huì)執(zhí)行while語句的內(nèi)容while(is.read(myArray)!=-1){for (int i = 0; i < myArray.length; i++) {System.out.print((char)myArray[i]);}}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 又遇到一個(gè)問題:當(dāng)使用數(shù)組的時(shí)候,如果文件里的字符的長(zhǎng)度不是數(shù)組的倍數(shù)的時(shí)候,拿到的數(shù)據(jù)會(huì)重復(fù)* */public static void test3() {byte[] myArray = new byte[3];int len;try {InputStream is = new FileInputStream("a.txt");//如果到達(dá)文件尾,無法滿足while的條件,不會(huì)執(zhí)行while語句的內(nèi)容while((len=is.read(myArray))!=-1){for (int i = 0; i < len; i++) {System.out.print((char)myArray[i]);}}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 試試使用數(shù)組輸出的會(huì)不會(huì)快一點(diǎn)* 自定義了一個(gè)要輸出的數(shù)組*/public static void test4() {System.out.println("讀取數(shù)據(jù)中");byte[] myArray = new byte[1024];int len;try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");FileOutputStream os = new FileOutputStream("22.wmv");//如果到達(dá)文件尾,無法滿足while的條件,不會(huì)執(zhí)行while語句的內(nèi)容while((len=is.read(myArray))!=-1){//定義一個(gè)新的數(shù)組,這個(gè)數(shù)據(jù)要防止有多條的數(shù)據(jù)byte descArray[] = new byte[len];for (int i = 0; i < len; i++) {descArray[i]=myArray[i];}os.write(descArray);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("成功");}/*** 試試使用數(shù)組輸出的會(huì)不會(huì)快一點(diǎn)* 使用jdk提供的方法,效率會(huì)更高一些*/public static void test5() {System.out.println("讀取數(shù)據(jù)中");byte[] myArray = new byte[1024];int len;try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");FileOutputStream os = new FileOutputStream("33.wmv");//如果到達(dá)文件尾,無法滿足while的條件,不會(huì)執(zhí)行while語句的內(nèi)容while((len=is.read(myArray))!=-1){//最后一次讀取數(shù)據(jù)的時(shí)候,只把從數(shù)組的第0個(gè)長(zhǎng)度開始到數(shù)組的指定len(我們從流中最后一次實(shí)際讀出的數(shù)據(jù)長(zhǎng)度)長(zhǎng)度os.write(myArray,0,len);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("成功");}/*** 使用緩沖區(qū),進(jìn)一步提供效率*/public static void test6() {try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");//把原來的流裝進(jìn)一個(gè)類里BufferedInputStream bis = new BufferedInputStream(is);FileOutputStream os = new FileOutputStream("34.wmv");byte myArray[] = new byte[1024];int len;while((len = bis.read(myArray))!=-1){os.write(myArray, 0, len);}} catch ( IOException e) {e.printStackTrace();}}/*** 使用輸出的緩沖區(qū),進(jìn)一步提供效率*/public static void test7() {try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");//把原來的流裝進(jìn)一個(gè)類里BufferedInputStream bis = new BufferedInputStream(is);FileOutputStream os = new FileOutputStream("35.wmv");BufferedOutputStream bos = new BufferedOutputStream(os);byte myArray[] = new byte[1024];int len;while((len = bis.read(myArray))!=-1){bos.write(myArray, 0, len);}bos.flush();bos.close();os.close();bis.close();is.close();} catch ( IOException e) {e.printStackTrace();}}/*** 當(dāng)我們沒有文件的時(shí)候,如果使用流*/public static void test8() {String content = "wangzongxing";ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());int a = 0;while((a = is.read())!=-1){System.out.print((char)a);}}/*** 新的問題:當(dāng)有中文的時(shí)候,可能會(huì)亂碼*/public static void test9() {String content = "你";byte myArray[] = content.getBytes();for (int i = 0; i < myArray.length; i++) {System.out.print((char)myArray[i]);}}/*** 新的問題:當(dāng)有中文的時(shí)候,可能會(huì)亂碼,當(dāng)有了流的時(shí)候。*/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);}}/*** 新的問題:當(dāng)有中文的時(shí)候,可能會(huì)亂碼,當(dāng)有了流的時(shí)候。*/public static void test11() {String content = "你";ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());byte a[] = new byte[1];try {is.read(a);System.out.println(new String(a));} catch (IOException e) {e.printStackTrace();}}public static void test12() {try {InputStream is = new FileInputStream("a.txt");byte myArray[] = new byte[12];int a = 0;while((a = is.read(myArray))!=-1){System.out.println(new String(myArray));}} catch (IOException e) {e.printStackTrace();}}/*** 引處字符流,來解決中文問題* Reader抽象類,InputStreamReader實(shí)現(xiàn)類*/public static void test13() {try {Reader reader = new InputStreamReader(new FileInputStream("a.txt"));int a = reader.read();System.out.println((char)a);reader.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 引處字符流,來解決中文問題,讀取到數(shù)組中,一次可以讀取多個(gè)字符* Reader抽象類,InputStreamReader實(shí)現(xiàn)類*/public static void test14() {try {Reader reader = new InputStreamReader(new FileInputStream("a.txt"));char myArray[] = new char[4];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();}}/*** 用字符流寫入數(shù)據(jù)到文件里*/public static void test15() {try {Writer writer = new OutputStreamWriter(new FileOutputStream("c.txt",true));writer.write("星哥哥");writer.flush();writer.close();// StringWriter} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 讀取字符流的時(shí)候,讀取的效率更高一些。*/public static void test16() {try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));while(reader.ready()){System.out.println(reader.readLine());}reader.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 讓寫入字符流更高效一些*/public static void test17() {try {BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d.txt")));bw.write("hello,星哥!123412341234");bw.flush();bw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** 為了能夠輸出txt數(shù)據(jù)的時(shí)候更方便,我們要引入printWriter.字符流*/public static void test18() {try {PrintWriter pw = new PrintWriter("e.txt");pw.print("I am 星哥!我");pw.print(18);pw.println("歲");pw.flush();pw.close();} catch (FileNotFoundException e) {e.printStackTrace();}}/*** 讀取txt文件的時(shí)候要更簡(jiǎn)單一些*/public static void test19() {Scanner sc;try {sc = new Scanner(new FileInputStream("e.txt"));while(sc.hasNextLine()){System.out.println(sc.nextLine());}sc.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/*** 新建一個(gè)文件*/public static void test20() {File file = new File("m.txt");try {System.out.println(file.createNewFile());} catch (IOException e) {e.printStackTrace();}}/*** 新建一個(gè)文件夾* @param args*/public static void test21() {File file = new File("mmm");System.out.println(file.mkdir());}/*** 刪除一個(gè)文件/文件夾*/public static void test22() {File file = new File("mmm");System.out.println(file.delete());}/*** 得命名一個(gè)文件/文件夾*/public static void test23() {File file = new File("nnn");System.out.println(file.renameTo(new File("abc")));}/*** 查看文件的權(quán)限* @param args*//*** 得命名一個(gè)文件/文件夾*/public static void test24() {File file = new File("abc");System.out.println(file.canRead());System.out.println(file.canWrite());System.out.println(file.canExecute());}/*** 修改文件/文件夾的權(quán)限*/public static void test25() {File file = new File("abc");System.out.println(file.setExecutable(false));System.out.println(file.canExecute());}/*** 搜索文件/文件夾的權(quán)限*/public static void test26() {File file = new File("./");String[] files = file.list();for (int i = 0; i < files.length; i++) {File temp = new File(files[i]);//if(files[i].contains(".jpg")){if(temp.isDirectory()){System.out.println("d"+files[i]+",是一個(gè)文件夾");}else{System.out.println("-"+files[i]+",是一個(gè)文件");}//}}}/*** 用系統(tǒng)提供的方法過濾文件* @param args*/public static void test27() {File file = new File("./");File files[] = file.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {if(pathname.getName().endsWith("doc")){return true;}return false;}});for (int i = 0; i < files.length; i++) {System.out.println(files[i].getName());}}public static void main(String[] args) {test27();}}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桂林市| 台江县| 宁蒗| 杭锦旗| 车致| 保德县| 农安县| 伊春市| 昆山市| 肥城市| 临江市| 遂宁市| 湘阴县| 五莲县| 开平市| 富顺县| 万载县| 大埔区| 忻城县| 肇庆市| 宜君县| 宁城县| 廉江市| 道孚县| 鹿泉市| 通州市| 青阳县| 台山市| 田东县| 礼泉县| 普兰县| 镇安县| 桂东县| 雷州市| 金门县| 磐石市| 巩义市| 平凉市| 平阴县| 昭苏县| 通化县|