JAVA|IO流的练习
IO
- 文件專屬
- FileInputStream-FileOutputStream
- FileReader-FileWriter
- 轉(zhuǎn)化流
- 緩沖流BufferedWriter-BufferedReader
- 數(shù)據(jù)流專屬
- 標(biāo)準(zhǔn)輸出流
- File類
- 文件拷貝
文件專屬
java.io.FileInputStream;java.io.FileOutputStream;java.io.FileReader;java.io.FileWriter;要自行構(gòu)造基本數(shù)據(jù)類型數(shù)組,來進(jìn)行讀寫,如char[] int[]
前面兩個(gè)有Stream的是對(duì)字節(jié)做處理,可以處理文本文檔(所有能被寫字板打開的文件)、照片、視頻、音頻等。
要注意的一點(diǎn)是Stream在寫入一個(gè)文件時(shí),要將文件目錄到具體文件名,不然將會(huì)拒絕訪問
然后每次對(duì)文件進(jìn)行處理如寫入新的內(nèi)容時(shí),要對(duì)文件進(jìn)行刷新——.flush()
在try語句中,最后要在finally處判斷文件是否為空,不為空時(shí)要將文件關(guān)閉,不然太占內(nèi)存。
FileInputStream-FileOutputStream
字節(jié)流
package com.hdujavaTest.IOTest;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; /*文件專屬 * 讀取字節(jié)流 不僅能處理文本文檔 還可以讀取寫入圖像等格式 因?yàn)榈讓邮菍?duì)字節(jié)做處理 * * */ public class FileInputStreamTest01 {public static void main(String[] args) {//文件字節(jié)輸入流 讀取文件String txt="C:\\Users\\wish_cai\\Pictures\\大學(xué)\\psb.jpg";fileInputStreamfuntion(txt);//文件寫入/*String txtwrite="D:\\JAVA\\java_test\\IOTest02.txt";fileOutputStreamfuntion(txtwrite);*///復(fù)制 文本文檔 還行 但是涉及圖片就拒絕訪問//之前的復(fù)制 是FileOutputStream是涉及到文件夾路徑C:\Users\wish_cai\Pictures\作業(yè),而FileOutputStream是寫入文件,所以要到具體的文件名String txtwritecopy="C:\\Users\\wish_cai\\Pictures\\作業(yè)\\新建文本文檔.jpg";fileCopy(txt,txtwritecopy);}//讀 輸入 硬盤到內(nèi)存public static String fileInputStreamfuntion(String txt){//idea的默認(rèn)路徑是在Project下//如果文件在其他模塊下,那么應(yīng)該讀取時(shí) 模塊\\src(根據(jù)實(shí)際)\\文件名//文檔字節(jié)輸入流String s=null;FileInputStream fileInputStream=null;try{/*read()一次讀取一個(gè)字節(jié) 并以int形式返回*//*System.out.println("一次讀取一個(gè)字節(jié) 并以int形式返回");fileInputStream=new FileInputStream(txt);int filer;while ((filer=fileInputStream.read())!=-1){System.out.print(filer+" ");}*//*設(shè)置一個(gè)字節(jié)數(shù)組 用read(byte[]) 讀取字節(jié),返回的是數(shù)量*///前面讀取完 之后需要重新新建一個(gè)FileInputStream對(duì)象//當(dāng)最后數(shù)組不夠時(shí),會(huì)覆蓋前面的,后面數(shù)組部分依舊會(huì)保留/*System.out.println('\n'+"設(shè)置一個(gè)字節(jié)數(shù)組 用read(byte[]) 讀取字節(jié),返回的是數(shù)量:");fileInputStream=new FileInputStream(txt);byte[] bytesarr=new byte[6];int num;while ((num=fileInputStream.read(bytesarr))!=-1){//System.out.println(num);String s=new String(bytesarr,0,num);System.out.print(s);//System.out.print(new String(bytesarr,0,num));}*///.available();//表示還有多少字節(jié)可用//System.out.println('\n'+"表示還有多少字節(jié)可用");fileInputStream=new FileInputStream(txt);//fileInputStream.available();//表示還有多少字節(jié)可用System.out.println(fileInputStream.available());byte[] bytesarr2=new byte[fileInputStream.available()];fileInputStream.read(bytesarr2);String s2=new String(bytesarr2,0,bytesarr2.length);//System.out.println(s2);s=s2;//跳過多少字節(jié)/* System.out.println("跳過多少字節(jié)");fileInputStream=new FileInputStream(txt);fileInputStream.skip(8);byte[] bytesarr3=new byte[fileInputStream.available()];fileInputStream.read(bytesarr3);String s3=new String(bytesarr3,0,bytesarr3.length);System.out.println(s3);*///} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileInputStream!=null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}return s;}//寫 輸出 內(nèi)存到硬盤 此處txt是我們的路徑public static void fileOutputStreamfuntion(String txt){FileOutputStream fileOutputStream=null;try{//謹(jǐn)慎使用 文件不存在 會(huì)自動(dòng)新建立路徑和內(nèi)容 文件之前存在 會(huì)覆蓋原內(nèi)容fileOutputStream=new FileOutputStream(txt);System.out.println("輸入需要寫的內(nèi)容:");Scanner scanner=new Scanner(System.in);String s=scanner.next();//String s="xinjianyigwenjianj.tsg";//s.getBytes();轉(zhuǎn)byte數(shù)組fileOutputStream.write(s.getBytes());//追加寫入fileOutputStream=new FileOutputStream(txt,true);System.out.println("追加寫入輸入需要寫的內(nèi)容:");Scanner scanner1=new Scanner(System.in);String s2=scanner1.next();//String s="xinjianyigwenjianj.tsg";//s.getBytes();轉(zhuǎn)byte數(shù)組fileOutputStream.write(s2.getBytes());//前面追加//沒有快捷鍵,那我的想法是 先讀取之前文件的,然后寫入我想加在開頭的,然后再將之前的寫入//寫完之后要刷新fileOutputStream.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileOutputStream!=null){try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}//文件的復(fù)制public static void fileCopy(String txtread,String txtwrite){FileInputStream fileInputStream=null;FileOutputStream fileOutputStream=null;try{//先輸入一個(gè)流對(duì)象fileInputStream=new FileInputStream(txtread);//讀fileOutputStream=new FileOutputStream(txtwrite);//寫//那就一邊讀 一邊寫int r;byte[] bytes=new byte[1024*1024];while ((r=fileInputStream.read(bytes))!=-1){fileOutputStream.write(bytes,0,r);}}catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileInputStream!=null){try{fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(fileOutputStream!=null){try{fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}} }FileReader-FileWriter
字符流
用字符流 讀取文本文件
但是只能處理文本文檔
轉(zhuǎn)化流
將字節(jié)流轉(zhuǎn)化為字符流
后面實(shí)用緩沖流的時(shí)候
我們可以不必自定義數(shù)組來輔助讀寫
而緩沖流的構(gòu)造方法的參數(shù)是
可見是Reader和Writer類型,而在字符流和字節(jié)流中,只有字符流 FileWriter和FileReader是符合的。
如果我們想要在緩沖流的構(gòu)造方法參數(shù)中傳入字節(jié)流 就要用到轉(zhuǎn)換流
緩沖流BufferedWriter-BufferedReader
**
緩沖流專屬
- 不需要自定義char數(shù)組,byte數(shù)組,自帶緩沖
- 將字符流 ————Reader下面的類FileReader 傳入包裝流的構(gòu)造方法
- FileReader extends InputStreamReader ;;;;; InputStreamReader extends Reader
- 而如果是字節(jié)流 FileInputStream 如何傳入呢
- 通過轉(zhuǎn)換流 轉(zhuǎn)化
- 可以一行一行讀
**
數(shù)據(jù)流專屬
能夠保存數(shù)據(jù)類型+實(shí)際內(nèi)容
且只能用對(duì)應(yīng)的數(shù)據(jù)流讀寫
DataOutputStream
標(biāo)準(zhǔn)輸出流
直接寫入到文件中
package com.hdujavaTest.IOTest;import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; //不需要手動(dòng)關(guān)閉close public class PrintStream01 {public static void main(String[] args) throws FileNotFoundException {System.out.println("標(biāo)準(zhǔn)輸出流——print");PrintStream ps=System.out;ps.println("adsad");ps.println(1);ps.println(true);//寫入到固定路徑的文件中PrintStream ps1=new PrintStream(new FileOutputStream("D:\\bilibili\\JJDown\\Download\\Java零基礎(chǔ)教程視頻(適合Java 0基礎(chǔ),Java初學(xué)入門)\\ad"));ps1.println(11111);ps1.println("ahuidqaas");} }File類
File和四大家族沒什么關(guān)系,所以File不能完成文件的讀和寫
*File對(duì)象代表什么?
- C:\ddd
- C:\dasdf\asf\asfasf\asfa.txt
- File對(duì)象可能是對(duì)應(yīng)的目錄也可能是文件
- File只是一個(gè)路徑的抽象表示形式
在對(duì)于文件的處理上也十分便捷。
提供了判斷文件路徑是否存在,不存在時(shí)新建,返回上一次修改的時(shí)間,刪除文件路徑、返回路徑的絕對(duì)形式、返回文件名、返回所有子文件的文件名(listFiles)等。
文件拷貝
java|IO流實(shí)現(xiàn)文件拷貝
package com.hdujavaTest.IOTest;import java.io.*;/***問題一:得到子文件夾的目錄之后,因?yàn)镕ileInputStream 需要讀到文件名 所以其拒絕訪問————遞歸調(diào)用* 涉及到 新建目錄** */ public class CopyFile01 {public static void main(String[] args) {File filesrc=new File("C:\\Users\\wish_cai\\Pictures\\作業(yè)");File filedest=new File("D:\\bilibili\\JJDown\\Download\\Java零基礎(chǔ)教程視頻(適合Java 0基礎(chǔ),Java初學(xué)入門)\\copy\\");CopyDir(filesrc,filedest);}private static void CopyDir(File filesrc, File filedest){if(filesrc.isFile()){//是文件先拷貝,再跳出該次遞歸,否則即為目錄,進(jìn)入目錄操作。//System.out.println(filesrc.getAbsolutePath());//是文件就進(jìn)行拷貝處理copyfile(filesrc,filedest);return;}File temFile=null;File[] files=filesrc.listFiles();for (File f:files) {//文件或目錄File newdest=null;File newsrc=null;if(f.isDirectory()){/*當(dāng)f是目錄中的一個(gè)子目錄時(shí) 進(jìn)入*/String name=f.getName();String destDir=filedest.getAbsolutePath().endsWith("\\")?(filedest.getAbsolutePath()+name):(filedest.getAbsolutePath()+"\\"+name);/*System.out.println(destDir);*///在拷貝路徑中生成新的目錄newdest=new File(destDir);if(!newdest.exists())newdest.mkdirs();/*String srcDir=f.getAbsolutePath().endsWith("\\")?f.getAbsolutePath():(f.getAbsolutePath()+"\\");*/String srcDir=f.getAbsolutePath();System.out.println(srcDir);newsrc=new File(srcDir);temFile=newdest;//更新拷貝路徑CopyDir(newsrc,temFile);}//temFile=newdest;/*當(dāng)目錄下的是一個(gè)文件時(shí) 進(jìn)入下一次循環(huán),然后直接拷貝*/if(!(f.isDirectory())){CopyDir(f,filedest);}}}private static void copyfile(File src,File dest){FileInputStream fileInputStream=null;FileOutputStream fileOutputStream=null;String txt=dest.getAbsolutePath().endsWith("\\")?dest.getAbsolutePath():(dest.getAbsolutePath()+"\\")+src.getName();System.out.println(txt);try{fileInputStream=new FileInputStream(src);fileOutputStream=new FileOutputStream(txt);byte[] bytes=new byte[1024*1024];int readcount;while ((readcount=fileInputStream.read(bytes))!=-1){fileOutputStream.write(bytes,0,readcount);}fileOutputStream.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileInputStream!=null) {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(fileOutputStream!=null){try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}} }總結(jié)
以上是生活随笔為你收集整理的JAVA|IO流的练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021全球Top 1000计算机科学家
- 下一篇: 大陆计算机科学家排名,韩家炜、张宏江2位