日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

JAVA|IO流的练习

發(fā)布時(shí)間:2023/12/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA|IO流的练习 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

IO

  • 文件專(zhuān)屬
    • FileInputStream-FileOutputStream
    • FileReader-FileWriter
  • 轉(zhuǎn)化流
  • 緩沖流BufferedWriter-BufferedReader
  • 數(shù)據(jù)流專(zhuān)屬
  • 標(biāo)準(zhǔn)輸出流
  • File類(lèi)
  • 文件拷貝

文件專(zhuān)屬

java.io.FileInputStream;java.io.FileOutputStream;java.io.FileReader;java.io.FileWriter;

要自行構(gòu)造基本數(shù)據(jù)類(lèi)型數(shù)組,來(lái)進(jìn)行讀寫(xiě),如char[] int[]
前面兩個(gè)有Stream的是對(duì)字節(jié)做處理,可以處理文本文檔(所有能被寫(xiě)字板打開(kāi)的文件)、照片、視頻、音頻等。
要注意的一點(diǎn)是Stream在寫(xiě)入一個(gè)文件時(shí),要將文件目錄到具體文件名,不然將會(huì)拒絕訪問(wèn)

然后每次對(duì)文件進(jìn)行處理如寫(xiě)入新的內(nèi)容時(shí),要對(duì)文件進(jìn)行刷新——.flush()
在try語(yǔ)句中,最后要在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; /*文件專(zhuān)屬 * 讀取字節(jié)流 不僅能處理文本文檔 還可以讀取寫(xiě)入圖像等格式 因?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);//文件寫(xiě)入/*String txtwrite="D:\\JAVA\\java_test\\IOTest02.txt";fileOutputStreamfuntion(txtwrite);*///復(fù)制 文本文檔 還行 但是涉及圖片就拒絕訪問(wèn)//之前的復(fù)制 是FileOutputStream是涉及到文件夾路徑C:\Users\wish_cai\Pictures\作業(yè),而FileOutputStream是寫(xiě)入文件,所以要到具體的文件名String txtwritecopy="C:\\Users\\wish_cai\\Pictures\\作業(yè)\\新建文本文檔.jpg";fileCopy(txt,txtwritecopy);}//讀 輸入 硬盤(pán)到內(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;//跳過(guò)多少字節(jié)/* System.out.println("跳過(guò)多少字節(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;}//寫(xiě) 輸出 內(nèi)存到硬盤(pán) 此處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("輸入需要寫(xiě)的內(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());//追加寫(xiě)入fileOutputStream=new FileOutputStream(txt,true);System.out.println("追加寫(xiě)入輸入需要寫(xiě)的內(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());//前面追加//沒(méi)有快捷鍵,那我的想法是 先讀取之前文件的,然后寫(xiě)入我想加在開(kāi)頭的,然后再將之前的寫(xiě)入//寫(xiě)完之后要刷新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);//寫(xiě)//那就一邊讀 一邊寫(xiě)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

字符流
用字符流 讀取文本文件
但是只能處理文本文檔

package com.hdujavaTest.IOTest;import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; /*文件專(zhuān)屬 用字符流 讀取文本文件(能用記事本打開(kāi)的) 復(fù)制文本文件 但是只能處理文本文檔 * */ public class Fileread01 {public static void main(String[] args) {/*String txt="D:\\JAVA\\java_test\\IOTest04copy.txt";FileReadfun(txt);*//*String txt1="C:\\Users\\wish_cai\\Pictures\\作業(yè)\\Write12.txt";FileWriterfun(txt1);*/String txt="D:\\JAVA\\java_test\\IOTest04copy.txt";String tet2="D:\\JAVA\\java_test\\IOrTow001.txt";CopyFile(txt,tet2);}public static void FileReadfun(String txt){FileReader fileReader=null;try{fileReader=new FileReader(txt);char[] chars=new char[30];int readnum;fileReader.read(chars);for (char c:chars) {System.out.print(c);}fileReader=new FileReader(txt);char[] chars2=new char[30];while ((readnum=fileReader.read(chars2))!=-1){System.out.println(new String(chars2,0,readnum));}fileReader=new FileReader(txt);while ((readnum=fileReader.read())!=-1){System.out.println(readnum);}fileReader=new FileReader(txt);}catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fileReader!=null){try {fileReader.close();} catch (IOException e) {e.printStackTrace();}}}}//只能寫(xiě)入普通文本 因?yàn)樘幚淼氖亲址? ,前面流是處理字節(jié)public static void FileWriterfun(String txt){FileWriter fileWriter=null;try{fileWriter=new FileWriter(txt);System.out.println("input need your contents");Scanner scanner=new Scanner(System.in);String x=scanner.nextLine();fileWriter.write(x);fileWriter.flush();FileReadfun(txt);//要刷新之后才能讀取} catch (IOException e) {e.printStackTrace();} finally {if(fileWriter!=null){try {fileWriter.close();} catch (IOException e) {e.printStackTrace();}}}}//復(fù)制 只能拷貝普通文本 先讀取 再寫(xiě)入public static void CopyFile(String readtxt,String writetxt){FileReader fileReader=null;FileWriter fileWriter=null;try{fileReader=new FileReader(readtxt);fileWriter=new FileWriter(writetxt);char[] rTow=new char[30];int numread=0;while ((numread=fileReader.read(rTow))!=-1){fileWriter.write(rTow);}fileWriter.close();FileReadfun(writetxt);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {}}}

轉(zhuǎn)化流

將字節(jié)流轉(zhuǎn)化為字符流

后面實(shí)用緩沖流的時(shí)候
我們可以不必自定義數(shù)組來(lái)輔助讀寫(xiě)
而緩沖流的構(gòu)造方法的參數(shù)是

public BufferedReader(Reader in) {this(in, defaultCharBufferSize);} public BufferedWriter(Writer out) {this(out, defaultCharBufferSize);}

可見(jiàn)是Reader和Writer類(lèi)型,而在字符流和字節(jié)流中,只有字符流 FileWriter和FileReader是符合的。
如果我們想要在緩沖流的構(gòu)造方法參數(shù)中傳入字節(jié)流 就要用到轉(zhuǎn)換流

OutputStreamWriter(new FileOutputStream("txt")) InputStreamReader(new FileInputStream("txt"));

緩沖流BufferedWriter-BufferedReader

**
緩沖流專(zhuān)屬

  • 不需要自定義char數(shù)組,byte數(shù)組,自帶緩沖
  • 將字符流 ————Reader下面的類(lèi)FileReader 傳入包裝流的構(gòu)造方法
  • FileReader extends InputStreamReader ;;;;; InputStreamReader extends Reader
  • 而如果是字節(jié)流 FileInputStream 如何傳入呢
  • 通過(guò)轉(zhuǎn)換流 轉(zhuǎn)化
  • 可以一行一行讀
    **
package com.hdujavaTest.IOTest;import java.io.*;/* * 緩沖流專(zhuān)屬 * 不需要自定義char數(shù)組,byte數(shù)組,自帶緩沖 * 將字符流 ————Reader下面的類(lèi)FileReader 傳入包裝流的構(gòu)造方法 * FileReader extends InputStreamReader ;;;;; InputStreamReader extends Reader * * 而如果是字節(jié)流 FileInputStream 如何傳入呢 * 通過(guò)轉(zhuǎn)換流 轉(zhuǎn)化* 可以一行一行讀 * */ public class Buffered01 {public static void main(String[] args) throws IOException {//緩沖流 讀 不需構(gòu)造特定數(shù)組String txt="D:\\JAVA\\java_test\\IOTest\\out\\production\\IOTest\\com\\hdujavaTest\\IOTest\\Fileread01.class";Bufferread(txt);//緩沖流 寫(xiě) 也不需構(gòu)造特定數(shù)組String txtwrite="C:\\Users\\wish_cai\\Pictures\\作業(yè)\\tete.txt";Bufferwirte(txtwrite);}public static void Bufferread(String txt) throws IOException {//字符流傳入緩沖流BufferedReader bufferedReader=null;FileReader fileReader=new FileReader(txt);bufferedReader=new BufferedReader(fileReader);//String x=bufferedReader.readLine();String x;while ((x=bufferedReader.readLine())!=null){System.out.println(x);}bufferedReader.close();//字節(jié)流傳入緩沖流 通過(guò)轉(zhuǎn)化流FileInputStream in=new FileInputStream(txt);//********字節(jié)流InputStreamReader inputStreamReader=new InputStreamReader(in);//***********通過(guò)轉(zhuǎn)換流 轉(zhuǎn)化為字符流BufferedReader bufferedReader1=null;bufferedReader1=new BufferedReader(inputStreamReader);//*****傳入bufferedReader1.close();/*在new 后面的構(gòu)造方法參數(shù)里 就是一個(gè)節(jié)點(diǎn)流* 用哪個(gè)構(gòu)造方法 其引用就是一個(gè)包裝流* *//*合并寫(xiě)*/BufferedReader bufferedReader2=new BufferedReader((new InputStreamReader(new FileInputStream(txt))));}public static void Bufferwirte(String txt) throws IOException {BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(txt));//字符流bufferedWriter.write("qweqhuiwfhais.!!!");//對(duì)文件作出修改 記得刷新bufferedWriter.flush();bufferedWriter.close();/*轉(zhuǎn)換流*/BufferedWriter bufferedWriter1=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txt,true)));//true 追加bufferedWriter1.write("\n"+"1111111111");bufferedWriter1.flush();bufferedWriter1.close();} }

數(shù)據(jù)流專(zhuān)屬

能夠保存數(shù)據(jù)類(lèi)型+實(shí)際內(nèi)容
且只能用對(duì)應(yīng)的數(shù)據(jù)流讀寫(xiě)
DataOutputStream

package com.hdujavaTest.IOTest;import java.io.*;public class DataStream01 {public static void main(String[] args) throws IOException {String txt1="D:\\bilibili\\JJDown\\Download\\Java零基礎(chǔ)教程視頻(適合Java 0基礎(chǔ),Java初學(xué)入門(mén))\\Data)";DataOutputStream(txt1);DataInputStream(txt1);}//數(shù)據(jù)字節(jié)輸入流 且?guī)в袛?shù)據(jù)基本類(lèi)型//且只能通過(guò)對(duì)應(yīng)的DataInputstream打開(kāi)public static void DataOutputStream(String txt) throws IOException {DataOutputStream dataOutputStream=new DataOutputStream(new FileOutputStream(txt));int a=1;int b=2;dataOutputStream.writeInt(a);dataOutputStream.writeInt(b);dataOutputStream.flush();dataOutputStream.close();}//讀取專(zhuān)屬數(shù)據(jù)流//而且要知道其專(zhuān)門(mén)的順序public static void DataInputStream(String txt) throws IOException {DataInputStream dataInputStream=new DataInputStream(new FileInputStream(txt));int x=dataInputStream.readInt();int y=dataInputStream.readInt();System.out.println(x);System.out.println(y);} }

標(biāo)準(zhǔn)輸出流

直接寫(xiě)入到文件中

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);//寫(xiě)入到固定路徑的文件中PrintStream ps1=new PrintStream(new FileOutputStream("D:\\bilibili\\JJDown\\Download\\Java零基礎(chǔ)教程視頻(適合Java 0基礎(chǔ),Java初學(xué)入門(mén))\\ad"));ps1.println(11111);ps1.println("ahuidqaas");} }

File類(lèi)

File和四大家族沒(méi)什么關(guān)系,所以File不能完成文件的讀和寫(xiě)
*File對(duì)象代表什么?

  • C:\ddd
  • C:\dasdf\asf\asfasf\asfa.txt
  • File對(duì)象可能是對(duì)應(yīng)的目錄也可能是文件
  • File只是一個(gè)路徑的抽象表示形式
    在對(duì)于文件的處理上也十分便捷。
    提供了判斷文件路徑是否存在,不存在時(shí)新建,返回上一次修改的時(shí)間,刪除文件路徑、返回路徑的絕對(duì)形式、返回文件名、返回所有子文件的文件名(listFiles)等。
public class File01 {public static void main(String[] args) {File file=new File("C:\\Users\\wish_cai\\Pictures\\作業(yè)\\");System.out.println("文件是否存在:"+file.exists());System.out.println(file.length());//不存在 以文件形式新建File file1=new File("C:\\Users\\wish_cai\\Pictures\\作業(yè)\\xinjian");if(!file1.exists()) {try {file1.createNewFile();} catch (IOException e) {e.printStackTrace();}}File file2=new File("C:\\Users\\wish_cai\\Pictures\\a\\b\\c\\");if(!file2.exists())file2.mkdirs();//mkdirs建立多重目錄 否則 mkdir只能新建一個(gè),在多重目錄要求下 沒(méi)有s將不會(huì)建立。} }

文件拷貝

java|IO流實(shí)現(xiàn)文件拷貝

package com.hdujavaTest.IOTest;import java.io.*;/***問(wèn)題一:得到子文件夾的目錄之后,因?yàn)镕ileInputStream 需要讀到文件名 所以其拒絕訪問(wèn)————遞歸調(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é)入門(mén))\\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流的练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。