目錄
一、前言
二、IO流概述
三、位元組流寫資料
位元組流寫資料的三種方式:
位元組流資料據
1.一次讀一個位元組
2.一次讀一個陣列
四、字符緩沖流
字符緩沖流寫檔案
字符緩沖流讀資料
一、前言
這些奇奇怪怪的流在理解起來也是其奇怪怪的,用起來卻真香┗|`O′|┛ 嗷~~
二、IO流概述
I表示input、O表示output表示輸入輸出流,
流是一種抽象的概念,是對資料傳輸的總稱,也就是資料在設備間傳輸稱為流,流的本質
是資料傳輸,流的本質是資料傳輸,
我們都知道,程式運行會加載到記憶體上,

- IO流就是用來處理設備間資料傳輸的問題的
IO流的分類大致可以分為兩種:
①按資料流向
- 輸入流:讀資料
- 輸出流:寫資料
②按資料型別分
- 位元組輸入流;位元組輸出流
- 字符輸入流;字符輸出流
一般來說IO流是按照資料型別分的
文本檔案一般用字符流,其他用位元組流
位元組流是萬能流,可以用于任意型別的檔案,
三、位元組流寫資料
①InputStream:
-
這個抽象類是表示輸入位元組流的所有類的超類,
②OutputStream:
-
這個抽象類是表示位元組輸出流的所有類的超類,
他們的子類都是以其父類作為子類名的后綴:


因為他是一個抽象類,所以我們寫、讀資料一般都用他們的直接子類,
FileOutputStream和FileInputStream,前者用于寫資料,后者用于讀資料,
FileOutputStream(String name):創建檔案輸入流以指定的名稱寫入檔案
public class FileOutputDemo {
public static void main(String[] args) throws IOException {
//創建位元組輸出流物件,這個子類都會拋出例外FileNotFoundException
FileOutputStream fos=new FileOutputStream("E:\\MyTest\\kong.txt");
File f1=new File("E:\\MyTest\\kong.txt");
System.out.println(f1.createNewFile());
//write會拋出IOException例外,所以他所在得方法中得拋出例外throws也可以try..catch處理
fos.write(49);//1
fos.write(48); //0
fos.write(48);//0
byte arr[]=new byte[10];
fos.write(arr);
//釋放資源
fos.close();
}
}
E:\\MyTest\\kong.txt,這個表示我創建在E盤下的Mytest模塊下得kong.txt目錄,當檔案
已經存會創建失敗(false),回傳的是boolean型別
創建物件做了三件事情:
- 呼叫系統功能創建檔案,
- 創建位元組輸出流物件,
- 讓位元組輸出流指向創建好的檔案,
凡是涉及到IO操做創建的物件都得釋放資源,
位元組流寫資料的三種方式:

import java.io.*;
public class FileOutputStreamDemo02 {
public static void main(String[] args) throws IOException {
//創建輸入流物件
FileOutputStream fos=new FileOutputStream("E:\\MyTest\\kong03.txt");
FileOutputStream fos1=new FileOutputStream(new File("E:\\MyTest\\kong03.txt"));
//上面兩種方式等價
//第一種方式寫
fos.write(97); fos.write(98); fos.write(99);
//第二種方式寫
byte[]bys={97,98,99,100};//abcd
fos.write(bys);
//第三種方式寫
byte[]bys1="abcd".getBytes();
fos.write(bys1, 1, 3);//bcd寫在檔案
//釋放資源
fos.close();
}
}

位元組流寫資料的兩個小問題:
有上面的操作可知,我們不難發現這寫入的資料沒有換行,這看著十分不舒服,
①位元組流輸入資料換行問題:
- windows作業系統下:\r\n
- Linux:\n
- mac:\r
只需在寫資料時加入對應的即可實作換行
②位元組流寫資料如何追加的問題
只需要在創建指定路徑后加true即可,
public FileOutputSteam(String name,boolean append)
import java.io.FileOutputStream;
import java.io.IOException;
public class FileTextZJ {
public static void main(String[] args) throws IOException {
FileOutputStream fos =new FileOutputStream("E:\\MyTest\\kong04.txt",true);
for (int i=0;i<5;i++){
fos.write("hello\r\n".getBytes());
}
fos.close();
}
}
下面時運行兩次的結果:,達到了我們像要的追加效果,

位元組流資料據
1.一次讀一個位元組
FileInputStream(String name)//寫出從哪里讀取
沒資料到達檔案末尾時,回傳-1,所以只需根據回傳值即可
public class FileInputStreamDemo{
public static void main(String [] args) throw IoException{
FileInputStream fis=new FileInputStream("E:MyByte\\kong02.txt");
int by;
while(by=fis.read()!=-1){
System.out.println((char)by);
}
fis.close();
}
}
2.一次讀一個陣列
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo01 {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("E:\\MyTest\\kong1.txt");
byte[]bys=new byte[1024];
int len;
while((len=fis.read())!=-1){
System.out.println(new String(bys,0,len));
}
fis.close();
}
}
四、字符緩沖流
- BufferedWriter
- BufferedReader

緩沖流的引數需要一個FileWriter或FileReader物件
字符緩沖流寫檔案
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo1 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\MyText\\KongChao.txt"));
bw.write("hello\r\n");
bw.write("world\r\n");
bw.close();
}
}
字符緩沖流讀資料
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("E:\\MyTest\\kong.txt"));
int ch;
while((ch=br.read())!=-1){
System.out.print((char)ch);
}
br.close();
}
}
字符緩沖流特有功能
- BufferedWriter:
void newLine();//相當于換行
- BufferedReader:
public String readLine():讀一行檔案,不包括換行符、和終止符,若流已經到檔案尾部,則為null
將使用特有方法,輸存結合
import java.io.*;
public class BufferedWriterDemo1 {
public static void main(String[] args) throws IOException {
//創建字符輸入流
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\MyTest\\KongChao.txt"));
//創建字符輸出流
BufferedReader br= new BufferedReader(new FileReader("E:\\MyTest\\copy.txt"));
String line;
while((line=br.readLine())!=null){
bw.write(line);bw.newLine();
bw.flush();//重繪到檔案上
}
//關閉資源
bw.close();
br.close();
} }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/384237.html
標籤:java
