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

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

io流,入門例子代碼

2019-11-10 23:24:59
字體:
來源:轉載
供稿:網友
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 目標文件*/public static void copyFile(String sourceName,String dest) {System.out.println("開始copy文件,源文件是:"+sourceName+",目標文件是:"+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);//這里只會正常寫入數據,不會把-1寫進去}fos.flush();//把緩沖區里的數據強制寫入到文件中fos.close();//關閉輸出流fis.close();//關閉輸入流} catch (IOException e) {e.printStackTrace();}}/*** 由于讀取速度太慢,所以要學習新的方法,讓我們更快一點*/public static void test2() {byte[] myArray = new byte[3];try {InputStream is = new FileInputStream("a.txt");//如果到達文件尾,無法滿足while的條件,不會執行while語句的內容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();}}/*** 又遇到一個問題:當使用數組的時候,如果文件里的字符的長度不是數組的倍數的時候,拿到的數據會重復* */public static void test3() {byte[] myArray = new byte[3];int len;try {InputStream is = new FileInputStream("a.txt");//如果到達文件尾,無法滿足while的條件,不會執行while語句的內容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();}}/*** 試試使用數組輸出的會不會快一點* 自定義了一個要輸出的數組*/public static void test4() {System.out.println("讀取數據中");byte[] myArray = new byte[1024];int len;try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");FileOutputStream os = new FileOutputStream("22.wmv");//如果到達文件尾,無法滿足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);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("成功");}/*** 試試使用數組輸出的會不會快一點* 使用jdk提供的方法,效率會更高一些*/public static void test5() {System.out.println("讀取數據中");byte[] myArray = new byte[1024];int len;try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");FileOutputStream os = new FileOutputStream("33.wmv");//如果到達文件尾,無法滿足while的條件,不會執行while語句的內容while((len=is.read(myArray))!=-1){//最后一次讀取數據的時候,只把從數組的第0個長度開始到數組的指定len(我們從流中最后一次實際讀出的數據長度)長度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("成功");}/*** 使用緩沖區,進一步提供效率*/public static void test6() {try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");//把原來的流裝進一個類里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();}}/*** 使用輸出的緩沖區,進一步提供效率*/public static void test7() {try {InputStream is = new FileInputStream("1-1輸入輸出流.wmv");//把原來的流裝進一個類里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();}}/*** 當我們沒有文件的時候,如果使用流*/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);}}/*** 新的問題:當有中文的時候,可能會亂碼*/public static void test9() {String content = "你";byte myArray[] = content.getBytes();for (int i = 0; i < myArray.length; i++) {System.out.print((char)myArray[i]);}}/*** 新的問題:當有中文的時候,可能會亂碼,當有了流的時候。*/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);}}/*** 新的問題:當有中文的時候,可能會亂碼,當有了流的時候。*/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實現類*/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();}}/*** 引處字符流,來解決中文問題,讀取到數組中,一次可以讀取多個字符* Reader抽象類,InputStreamReader實現類*/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();}}/*** 用字符流寫入數據到文件里*/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();}}/*** 讀取字符流的時候,讀取的效率更高一些。*/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數據的時候更方便,我們要引入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文件的時候要更簡單一些*/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();}}/*** 新建一個文件*/public static void test20() {File file = new File("m.txt");try {System.out.println(file.createNewFile());} catch (IOException e) {e.printStackTrace();}}/*** 新建一個文件夾* @param args*/public static void test21() {File file = new File("mmm");System.out.println(file.mkdir());}/*** 刪除一個文件/文件夾*/public static void test22() {File file = new File("mmm");System.out.println(file.delete());}/*** 得命名一個文件/文件夾*/public static void test23() {File file = new File("nnn");System.out.println(file.renameTo(new File("abc")));}/*** 查看文件的權限* @param args*//*** 得命名一個文件/文件夾*/public static void test24() {File file = new File("abc");System.out.println(file.canRead());System.out.println(file.canWrite());System.out.println(file.canExecute());}/*** 修改文件/文件夾的權限*/public static void test25() {File file = new File("abc");System.out.println(file.setExecutable(false));System.out.println(file.canExecute());}/*** 搜索文件/文件夾的權限*/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]+",是一個文件夾");}else{System.out.println("-"+files[i]+",是一個文件");}//}}}/*** 用系統提供的方法過濾文件* @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();}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 增城市| 铁岭市| 曲松县| 苍山县| 绥中县| 广昌县| 遵义县| 渑池县| 安塞县| 莱阳市| 右玉县| 芮城县| 大连市| 临海市| 双辽市| 永靖县| 天等县| 那曲县| 和顺县| 准格尔旗| 武宣县| 西乌珠穆沁旗| 昭通市| 达拉特旗| 武清区| 临安市| 静宁县| 宜春市| 志丹县| 苍梧县| 左权县| 丹寨县| 神池县| 武宣县| 乌鲁木齐县| 邻水| 太白县| 安陆市| 宜兰市| 聊城市| 兖州市|