最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Java對IO中每個流的理解心得,字符流字節(jié)流,轉(zhuǎn)換流復(fù)制,對象流,分割流【詩書畫唱】

2020-07-21 23:01 作者:詩書畫唱  | 我要投稿

寫一下你對IO流的心得,對于每個流的理解



如果我們把水當(dāng)做文件,把水工廠當(dāng)做file類,將運輸用的大小水管當(dāng)做inputstream,outputstream,reader,writer,再把瓢子,盤,水桶等容器當(dāng)做數(shù)組,char等。file類是可以放很多很多的水的大倉庫,通過inputstream,outputstream,reader,writer這四個節(jié)點流(=水管),將水運輸?shù)缴鐓^(qū),最后,用不同容器(=數(shù)組,char)將水裝出來用。


字節(jié)流:一次讀入或讀出是8位二進制

字符流:一次讀入或讀出是16位二進制

JDK 中后綴是 Stream 是字節(jié)流;后綴是 Reader,Writer 是字符流

節(jié)點流:直接與數(shù)據(jù)源相連,讀入或?qū)懗?/p>

處理流:與節(jié)點流一塊使用,在節(jié)點流的基礎(chǔ)上,再套接一層

?

常用的流

  1. 對文件進行操作:FileInputStream(字節(jié)輸入流)、FileOutputStream(字節(jié)輸出流)、FileReader(字符輸入流)、FileWriter(字符輸出流)

  2. 對管道進行操作:PipedInputStream(字節(jié)輸入流)、PipedOutStream(字節(jié)輸出流)、PipedReader(字符輸入流)、PipedWriter(字符輸出流)

  3. 字節(jié)/字符數(shù)組:ByteArrayInputStream、ByteArrayOutputStream、CharArrayReader、CharArrayWriter

  4. Buffered 緩沖流:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter

  5. 字節(jié)轉(zhuǎn)化成字符流:InputStreamReader、OutputStreamWriter

  6. 數(shù)據(jù)流:DataInputStream、DataOutputStream

  7. 打印流:PrintStream、PrintWriter

  8. 對象流:ObjectInputStream、ObjectOutputStream

  9. 序列化流:SequenceInputStream



使用字符流(就是用上BufferedReader等)復(fù)制一個文本文件


//字節(jié)流:

// FileInputStream:字節(jié)輸入流

// FileOutputStream:字節(jié)輸出流

// 字節(jié)流是可以讀取任何文件,每次讀取的時候是1字節(jié)

//緩沖流(包裝流):

// BufferedReader:字符輸入緩沖流

// BufferedWriter:字符輸出緩沖流

// BufferedInputStream:字節(jié)輸入緩沖流

// BufferedOutputStream:字節(jié)輸出緩沖流

//為了提高讀寫流的效率,引入了緩沖機制,

//進行批量的讀寫,提高了讀寫的效率。

//Buffered包裝類用于加快了讀寫內(nèi)容的速度轉(zhuǎn)換流:

// 兩個功能:1.將輸入的字節(jié)轉(zhuǎn)換為字符

// 2.進行編碼轉(zhuǎn)換

// InputStreamReader:字節(jié)輸入轉(zhuǎn)換流

// OutputStreamWriter:字節(jié)輸出轉(zhuǎn)換流

package all;


import java.io.*;


public class copy {

public static void main(String[] args) throws IOException {

File old = new File("xiangDui.txt");

File copy = new File("xiangDuiCopy.txt");


CopyFangFa(old,copy);

}


public static void CopyFangFa(File old,File copy)?

throws IOException {

//用FileInputStream等來創(chuàng)建字節(jié)輸入輸出流對象:

FileInputStream byteInput = new FileInputStream(old);

FileOutputStream byteOut = new FileOutputStream(copy);


//復(fù)制過程用字節(jié)數(shù)組和write寫入來實現(xiàn):

// 用每次讀取1024的字節(jié)的話,執(zhí)行所花的時間就會變得更少。

int len;

byte[] byteArray = new byte[1024];

while ((len = byteInput.read(byteArray)) != -1) {

byteOut.write(byteArray, 0, len);

}


//用close,關(guān)閉,之后可釋放資源:

byteOut.close();

byteInput.close();

}

}









使用字節(jié)流(FileInputStream等)復(fù)制一個圖片




package IO;


import java.io.*;


public class copyImg {

public static void main(String[] args) throws Exception {

File oldFile=new File("7.jpg");


BufferedInputStream CharInput=

new BufferedInputStream(

new FileInputStream(oldFile));

BufferedOutputStream CharOut=

new BufferedOutputStream(new FileOutputStream("7Copy.jpg"));


byte[] byteArray=new byte[1024];

int length=0;

while((length=CharInput.read(byteArray))!=-1){

CharOut.write(byteArray,0,length);

}

CharOut.flush();

CharOut.close();


CharInput.close();


System.out.println("復(fù)制成功!");

}}





使用緩沖流復(fù)制一個文件夾和里面的所有文件



package all;


import java.io.*;



public class copyWenJianjia {


? public static void copyAllFanFa(File oldFile, File copyFile)?

?throws Exception{

??

? ? ? ? ? //用isDirectory判斷原來的老的文件是否是文件夾

? ? ? ? ? if (oldFile.isDirectory()) {

? ? ? ? ? ? ? // 被復(fù)制的文件沒復(fù)制成功,不存在,就創(chuàng)建文件夾

? ? ? ? ? ? ? if (!copyFile.exists()) {

? ? ? ? ? ? ? ? ? copyFile.mkdir();

? ? ? ? ? ? ? }

? ? ? ? ? ? ? // 將文件夾下的文件存入文件數(shù)組StringArray(字符串?dāng)?shù)組)

? ? ? ? ? ? ? String[] StringArray = oldFile.list();

? ? ? ? ? ? ? for (String File : StringArray) {

? ? ? ? ? ? ? ? ? //用new等創(chuàng)建文件夾下的子目錄,src:路徑或目錄

? ? ? ? ? ? ? ? ? File oldFileSrc = new File(oldFile, File);

? ? ? ? ? ? ? ? ? File copyFileSrc = new File(copyFile, File);

? ? ? ? ? ? ? ? ? // 個人的理解:在自身方法中調(diào)用自身的方法,

//? ? ? ? ? ? ? ? ? 這樣的話調(diào)用一次方法,就是調(diào)用了

//? ? ? ? ? ? ? ? ? 很多次正在循環(huán)執(zhí)行的方法

//? ? ? ? ? ? ? ? ? 直到文件等遍歷完了,就是將文件進行下一層循環(huán)。

? ? ? ? ? ? ? ? ? copyAllFanFa(oldFileSrc, copyFileSrc);

? ? ? ? ? ? ? }

? ? ? ? ? } else {



? ? ? ? ? ? ? // 創(chuàng)建FileInputStream(文件輸入字節(jié)流)

//? ? ? ? :? 用于讀取文件內(nèi)容,源文件

? ? ? ? ? ? ? FileInputStream byteInput = new FileInputStream(oldFile);


? ? // 創(chuàng)建FileOutputStream(文件輸出的字節(jié)流)

//? ? ? ? ? ? ? ,用于將讀取到的文件內(nèi)容

//? ? ? ? ? ? ? 寫到另一個磁盤文件中,復(fù)制目標文件等

? ? ? ? ? ? ? FileOutputStream byteOut =

? ? ? ? ? ? ? new FileOutputStream(copyFile);


? ? ? ? ? ??


? ? ? ? ? ? ? int len = -1;

? ? ? ? ? ? ? byte[] byteArray = new byte[1024];

? ? ? ? ? ? ? while (true) {

?// 從FileInputStream(文件輸入流)

//? ? ?中讀取數(shù)據(jù)。每執(zhí)行一次,數(shù)據(jù)讀到字節(jié)數(shù)組b中

? ? ? ? ? ? ? ? ? len = byteInput.read(byteArray, 0, 256);

? ? ? ? ? ? ? ? ? if (len == -1) {

? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? }

//? ? ? ? ? ? ? ? ? System.out.println(byteArray.toString());

? ? ? ? ? ? ? ? ? byteOut.write(byteArray);

? ? ? ? ? ? ? }

//? ? ? ? ? ? ? byteOut.write("\r\n".getBytes()); // 換行

? ? ? ? ? ? ? byteInput.close();

? ? ? ? ? ? ? byteOut.close();

? ? ? ? ? }


? ? ??


? }

? public static void main(String[] args)?

throws Exception{

? ? ? copyAllFanFa(new File("NewFile"),new File("NewCopyFile"));

? }

}








使用轉(zhuǎn)換流復(fù)制一個文件夾下的所有txt文件,將文件的編碼格式轉(zhuǎn)換為utf-8




package all;


import java.io.*;



public class copyAlltxt {


? public static void copyAllFanFa(File oldFile, File copyFile)?

?throws Exception{

??

? ? ? ? ? if (oldFile.isDirectory()) {

? ? ? ? ? ??

? ? ? ? ? ? ? if (!copyFile.exists()) {

? ? ? ? ? ? ? ? ? copyFile.mkdir();

? ? ? ? ? ? ? }

? ? ? ? ? ??

? ? ? ? ? ? ? String[] StringArray = oldFile.list();

? ? ? ? ? ? ? for (String File : StringArray) {

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? File oldFileSrc = new File(oldFile, File);

? ? ? ? ? ? ? ? ? File copyFileSrc = new File(copyFile, File);


? ? ? ? ? ? ? ? ? copyAllFanFa(oldFileSrc, copyFileSrc);

? ? ? ? ? ? ? }

? ? ? ? ? } else {



? ? ? ? ? ? ? FileInputStream byteInput =?

? ? ? ? ? ? ? new FileInputStream(oldFile);


? ? ? InputStreamReader byteChangeInput=

? ? ? new InputStreamReader(byteInput,"UTF-8");

//? ? ? InputStreamReader:字節(jié)輸入轉(zhuǎn)換流(個人的理解:

//? ? ? 把一些內(nèi)容轉(zhuǎn)格式后保存。)

? ? ? ? ? ? ? FileOutputStream byteOut =

? ? ? ? ? ? ? new FileOutputStream(copyFile);


? ? ? ? ? ? ? char[] charArray=new char[100];

? ? ? int len;

? ? ? byte[] byteArray= new byte[1024];

? ? ? while((len=byteChangeInput.read(charArray))!=-1){


? ? ? byteOut.write(byteArray);

? ? ? }

? ? ? byteChangeInput.close();? ? ? ? ? ?


? ? ? ? ? ? ? byteInput.close();

? ? ? ? ? ? ? byteOut.close();

? ? ? ? ? }


? ? ??


? }

? public static void main(String[] args)?

throws Exception{

? ? ? copyAllFanFa(new File("NewFile"),new File("NewCopyFile"));

? }

}






使用分割流復(fù)制一個文本文件



//字節(jié)流:


// FileInputStream:字節(jié)輸入流


// FileOutputStream:字節(jié)輸出流


// 字節(jié)流是可以讀取任何文件,每次讀取的時候是1字節(jié)


//緩沖流(包裝流):


// BufferedReader:字符輸入緩沖流


// BufferedWriter:字符輸出緩沖流


// BufferedInputStream:字節(jié)輸入緩沖流


// BufferedOutputStream:字節(jié)輸出緩沖流


//為了提高讀寫流的效率,引入了緩沖機制,


//進行批量的讀寫,提高了讀寫的效率。


//Buffered包裝類用于加快了讀寫內(nèi)容的速度轉(zhuǎn)換流:


// 兩個功能:1.將輸入的字節(jié)轉(zhuǎn)換為字符


// 2.進行編碼轉(zhuǎn)換


// InputStreamReader:字節(jié)輸入轉(zhuǎn)換流


// OutputStreamWriter:字節(jié)輸出轉(zhuǎn)換流


package fenGeLiu;




import java.io.*;




public class copy {


public static void main(String[] args) throws IOException {


File old = new File("xiangDui.txt");


File copy = new File("xiangDuiCopy.txt");




CopyFangFa(old,copy);


}




public static void CopyFangFa(File old,File copy)?


throws IOException {


//用FileInputStream等來創(chuàng)建字節(jié)輸入輸出流對象:

RandomAccessFile byteInput=new RandomAccessFile(old,"r");

//r:是固定不可變的“key”一般的,代表read的部分

RandomAccessFile byteOut=new RandomAccessFile(copy,"rw");

//rw:是固定不可變的“key”一般的,代表read后的write的部分

//——————————————————

//FileInputStream byteInput = new FileInputStream(old);

//

//FileOutputStream byteOut = new FileOutputStream(copy);


//——————————————————————


//復(fù)制過程用字節(jié)數(shù)組和write寫入來實現(xiàn):


// 用每次讀取1024的字節(jié)的話,執(zhí)行所花的時間就會變得更少。


int len;


byte[] byteArray = new byte[1024];


while ((len = byteInput.read(byteArray)) != -1) {


byteOut.write(byteArray, 0, len);


}




//用close,關(guān)閉,之后可釋放資源:


byteOut.close();


byteInput.close();


}


}






使用對象流保存一個對象的信息




package fenGeLiu;

import java.io.*;

import java.util.Date;

public class baoLiuOneObject {

public static void main(String[] args) throws Exception{


student duiXiang1=new student(666,"詩書畫唱三連關(guān)注",100.0f);


File File=new File("new.txt");


FileOutputStream FileOutput=new FileOutputStream(File);


ObjectOutputStream ObjectOutput

=new ObjectOutputStream(FileOutput);

ObjectOutput.writeObject(duiXiang1);


ObjectOutput.writeObject(null);

ObjectOutput.close();

System.out.println("存儲成功");



// 下面是打印所有的存在new.txt的文件的對象的內(nèi)容:

// File File=new File("new.txt");


//傳入字節(jié)輸入流:

FileInputStream byteInputAll=new FileInputStream(File);


// duiXiangInput【“拼音+英文”的自己的命名】(對象輸入流)。

ObjectInputStream duiXiangInput

=new ObjectInputStream(byteInputAll);

Object duiXiang="";

while((duiXiang=duiXiangInput.readObject())!=null){

System.out.println(duiXiang.toString());

}

duiXiangInput.close();

}

}


class student implements Serializable{


int bianHao;

String name;

// transient float chengJi;

float chengJi;

// 這里用transient來讓成績(chengJi)不參與序列化,

// 就是后面用toString方法返回的時候,

// 不會打印出傳入的真實成績放的內(nèi)容。

public student(int bianHao, String name, float chengJi) {

this.bianHao = bianHao;

this.name = name;

this.chengJi = chengJi;

}

@Override

public String toString() {

return "編號:" +?

bianHao + ", 姓名:" + name + ", 成績("

+ "若該變量數(shù)據(jù)類型前"

+ "\n設(shè)置為transient,就不能打印傳入的內(nèi)容"

+ "\n出來的成績默認為0.0):" +chengJi+ "\n";

}


}



Java對IO中每個流的理解心得,字符流字節(jié)流,轉(zhuǎn)換流復(fù)制,對象流,分割流【詩書畫唱】的評論 (共 條)

分享到微博請遵守國家法律
东台市| 新蔡县| 保德县| 芮城县| 环江| 西青区| 宝兴县| 巨鹿县| 广丰县| 秦皇岛市| 马龙县| 盈江县| 安徽省| 青海省| 北票市| 安庆市| 新竹市| 吉林市| 理塘县| 黔东| 棋牌| 广平县| 黑山县| 钟山县| 湘乡市| 云浮市| 九龙县| 安丘市| 德庆县| 永州市| 平顶山市| 新津县| 海晏县| 武鸣县| 射洪县| 益阳市| 亳州市| 拜城县| 冷水江市| 宜城市| 瑞安市|