Java IO流相關(guān)知識代碼解析
一、IO流的分類
字符流
Reader
InputStreamReader(節(jié)點(diǎn)流)
BufferedReader(處理流)
Writer
OutputStreamWriter(節(jié)點(diǎn)流)
BufferedWriter(處理流)
PrintWriter
字節(jié)流
InputStream
FileInputStream(節(jié)點(diǎn)流)
BufferedInputStream(處理流)
ObjectInputStream(處理流)
PrintStream
OutputStream
FileOutputStream(節(jié)點(diǎn)流)
BufferedOutputStream(處理流)
ObjectOutputStream(處理流)
斷點(diǎn)處理的流
RandomAccessfile
二、IO流的用法
1、轉(zhuǎn)換流的用法
FileInputStream in = new FileInputStream(newFile(""));
Readerreader = new InputStreamReader(in);//字節(jié)轉(zhuǎn)字符
FileOutputStreamout = new FileOutputStream(newFile(""));
Writer writer = new OutputStreamWriter(out);//字符轉(zhuǎn)字節(jié)
2、對象序列化,對象需要實(shí)現(xiàn)Serializable接口
FileOutputStreamfileOutputStream = new FileOutputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(object);//向指定文件寫入對象object
objectOutputStream.close();
FileInputStreamfileInputStream = new FileInputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream);
object = objectInputStream.readObject();//讀取得到對象object
fileInputStream . lose();
3、斷點(diǎn)的運(yùn)用
public class Copy extends Thread{
//可以利用多線程實(shí)現(xiàn)拷貝
longstart;
longend;
Filesorce;
Filetargetdir;
publicCopy() {
}
publicCopy(longstart,long end, File sorce, File targetdir) {
//利用構(gòu)造方法傳遞需要拷貝的長度,拷貝開始位置,以及目標(biāo)文件和源文件
super();
this.start= start;
this.end= end;
this.sorce= sorce;
this.targetdir= targetdir;
}
@Override
publicvoid run(){
try{
RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r");
RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw");
souceRaf.seek(start);
targetRaf.seek(start);
intlen= 0;
byte[]bs = new byte[1024];
longseek;
System.out.println(start+"---->"+end+this.getName());
while((len= souceRaf.read(bs))!=-1){
targetRaf.write(bs, 0, len);
seek= souceRaf.getFilePointer();
//獲取斷點(diǎn)位置
if(seek== end){
break;
}
}
targetRaf.close();
souceRaf.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
4、字節(jié)流的用法
public class Test_InputStream {
//利用字節(jié)流獲取文本文件內(nèi)容,但是容易出現(xiàn)問題
/*
//可能出現(xiàn)int長度越界
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt"));
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
String str = new String(b);
System.out.println(str);
}
*/
//可能出現(xiàn)亂碼
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt");
InputStream inputStream = new FileInputStream(file);
//統(tǒng)計(jì)每次讀取的實(shí)際長度
int len = 0;
//聲明每次讀取1024個(gè)字節(jié)
byte[] b = new byte[2];
StringBuffer sBuffer = new StringBuffer();
while((len=inputStream.read(b))!=-1){
sBuffer.append(new String(b,0,len));
}
System.out.println(sBuffer.toString());
}
}
//利用字節(jié)流拷貝文件
public void copy(File sourceFile, File targetDir) {
//
FileInputStreamfileInputStream = null;
FileOutputStreamfileOutputStream = null;
fileInputStream= new FileInputStream(sourceFile);
FiletargetFile = new File(targetDir,sourceFile.getName());
fileOutputStream= new FileOutputStream(targetFile);
byte[]b = new byte[1024];
intlen = 0;
while((len= fileInputStream.read(b)) != -1) {
fileOutputStream.write(b, 0, len);
}
}
5、緩存字符流的用法
publicstatic void main(String[] args) throws IOException {
//緩存字符流實(shí)現(xiàn)寫入文件
InputStreamin = System.in;
Readerreader = new InputStreamReader(in);
BufferedReaderbr = new BufferedReader(reader);
BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt")));
Strings="";
while((s=br.readLine())!=null) {
bw.write(s);
bw.newLine();
bw.flush();
//字符流千萬不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
總結(jié)
以上就是本文關(guān)于Java IO流相關(guān)知識代碼解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
如何在 Spring Boot 中配置和使用 CSRF 保護(hù)
CSRF是一種網(wǎng)絡(luò)攻擊,它利用已認(rèn)證用戶的身份來執(zhí)行未經(jīng)用戶同意的操作,Spring Boot 提供了內(nèi)置的 CSRF 保護(hù)機(jī)制,可以幫助您防止這種類型的攻擊,這篇文章主要介紹了Spring?Boot?中的?CSRF?保護(hù)配置的使用方法,需要的朋友可以參考下2023-09-09
idea 實(shí)現(xiàn)git rebase操作應(yīng)用場景
本文結(jié)合idea工具進(jìn)行rebase的各種場景的操作,借助工具更能直觀地觀察到分支之間地操作差異,方便我們理解rebase的各種操作以及場景的使用,對idea git rebase操作知識感興趣的朋友一起看看吧2024-01-01
Spring中@Validated和@Valid區(qū)別淺析
@Valid是javax.validation里的,?@Validated是@Valid?的一次封裝,是Spring提供的校驗(yàn)機(jī)制使用,下面這篇文章主要給大家介紹了關(guān)于Spring中@Validated和@Valid區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-04-04
spring學(xué)習(xí)之參數(shù)傳遞與檢驗(yàn)詳解
這篇文章主要給大家介紹了關(guān)于spring參數(shù)傳遞與檢驗(yàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作能帶來一定的幫助,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
Java 中的FileReader和FileWriter源碼分析_動力節(jié)點(diǎn)Java學(xué)院整理
本文給大家分享一段示例程序,通過示例代碼可以看出FileReader是基于InputStreamReader實(shí)現(xiàn)的,FileWriter是基于OutputStreamWriter實(shí)現(xiàn)的,具體程序代碼大家通過本文了解下吧2017-05-05
java實(shí)現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法
這篇文章主要介紹了java實(shí)現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法,可實(shí)現(xiàn)針對圖片大小的縮放功能,是Java針對圖片操作的典型應(yīng)用,需要的朋友可以參考下2014-11-11
SpringBoot 動態(tài)定時(shí)器的使用方法
這篇文章主要介紹了SpringBoot 動態(tài)定時(shí)器的使用方法,非常不錯(cuò),具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2018-05-05

