一,什么是IO流
io是Input和Output的簡稱。Input代表輸入,Output代表輸出。輸入輸出都是以內存為參照
數據從(網絡,硬盤...)進入內存稱為輸入,從內存出去稱為輸出。
IO流的分類
- 按照流的方向
- 數據
進入內存輸入流
- 數據
離開內存輸出流
- 數據
- 按處理的數據的方式
- byte字節讀取,
字節流
- 萬能流,可以讀取任何文件,視頻,音頻,txt,xslx,
- 字符讀取
字符流
- 適合讀取txt文本文件
- byte字節讀取,
- 處理數據類型
- 特點
- 所有的以
Stream結尾的都是字節流
,以reader/writer結尾的都是字符流
- 所有的流所有的流都實現了Java.io.Closeable接口,都有
close()
方法。 - 所有
輸出流
都有flush()
方法。
- 所有的以
流的具體介紹
1.字節文件輸入/輸出流
-
輸入流
- 構造方法
- 構造流的時候需要傳入一個路徑,或文件代表的路徑來實現連接。
- 常用方法
-
int read() 從此輸入流中讀取一個數據字節。 int read(byte[] b) 從此輸入流中將最多 b.length 個字節的數據讀入一個字節數組中。 int read(byte[] b, int off, int len) 從此輸入流中將最多 len 個字節的數據讀入一個字 節數組中。 long skip(long n) 從輸入流中跳過並丟棄 n 個字節的數據。
- 寫的 時候是把
特定byte大小的數組寫入輸入流中。
-
- 構造方法
-
輸出流
-
構造方法
- FileOutPutStream(String name,boolean append)后面的布爾值表示是否往
具體文件
中追加文件false就會清空源文件然后寫入。true表示在原具體文件
的末尾追加。
-
常用方法
-
void write(byte[] b) 將 b.length 個字節從指定字節數組寫入此文件輸出流中。 void write(byte[] b, int off, int len) 將指定字節數組中從偏移量 off 開始的 len 個字節寫入此文件輸出流 。 void write(int b) 將指定字節寫入此文件輸出流。
-
-
-
例子
public class FileInputstream05{ public static void main (String[]args) { FileInputStream fis = null; try{ fis = new FileInputStream("src/tempfile"); byte[]bytes = new byte[4]; //循環讀取文件內容 /*while(true){ //每次讀取一個byte數組長度 intreadcount=fis.read(bytes); //讀取不到文件內容時停止循環 if(readcount==-1){ break; } System.out.print(newString(bytes,0,readcount)); }*/ int readcount = 0; while((readcount=fis.read(bytes))!=-1){ //把讀取到的數據轉換為字符串,讀多少轉換多少個。 System.out.print(new String(bytes,0,readcount)); } }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } finally{ //流不為空的時候關閉。 if(fis!=null){ try{ fis.close(); }catch(IOExceptione){ e.printStackTrace(); } } ========================================================================= publicstaticvoidmain(String[]args){ FileOutputStreamfos=null; try{ //如果沒有該文件就會創建一個新的。並且會每次清除原來的然后寫。 //加入true之后就會在原來的位置末尾添加寫。 fos=newFileOutputStream("myfile",true); byte[] bytes ={96,97,98,99}; fos.write(bytes); String s ="我是一個好少年"; //字符串轉為數組 byte[] bytes1 = s.getBytes(); fos.write(bytes1); //寫完之后一定要刷新。 fos.flush(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ if(fos!=null){ try{ fos.close(); }catch(IOExceptione){ e.printStackTrace(); }
}
}
```
2.字符文件輸入輸出流
- 主要處理文本文件---
可以直接由文本文件打開的
。.txt .java - 方法繼承自字節流,只是承載容器由byte數組換為了
char[]字節數組
。
publicstaticvoidmain(String[]args){
FileReaderreader=null;
try{
//構建一個字符讀取對象
reader= new FileReader("src/tempfile");
char[]chars=new char[4];
intreaderCount=0;
while((readerCount=reader.read(chars))!=-1){
System.out.println(newString(chars,0,readerCount));
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione){
e.printStackTrace();
=========================================================================================
publicstaticvoidmain(String[]args){
FileWriterwriters=null;
try{
//可以在后面追加也可以清空后填寫;
writers=newFileWriter("src/tempfile",true);
char[]chars={'我','是','中','國','人'};
Stringstrings="我愛我的祖國";
//可以寫入字符數組,也能寫入字符串。
writers.write(chars);
writers.write(strings);
//使用后一定要刷新
writers.flush();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(writers!=null){
try{
writers.close();
}catch(IOExceptione){
e.printStackTrace();
}

3.緩沖流
-
緩沖流包括字節和字符流
-
BufferFileReader
-
BufferFileWriter
-
BufferFileInputStream
-
BufferFileOutputStream
-
-
構造方法,需要傳入一個基本流
- 構造的時候可以指定一個緩沖區的大小,不指定
默認8KB
- 構造的時候可以指定一個緩沖區的大小,不指定
-
原理
- 緩沖流就是先把讀取到的數據放入自己的緩沖區域當中,減少硬盤與內存之間的訪問次數。
- 猜想普通流傳輸的時候是把數據放入管道,但是占用了連接,緩沖區可以減少連接的占用
- 讀取的時候可以默認讀取一個字節或者指定字節讀取。
BufferReader
有一個特有方法readLine()
可以一次讀取一行文本數據。
-
例子
/*
BufferedReader:字符緩沖流,自帶緩沖,
不需要再建立byte[],char[]數組。
也可以建立數組,把數據存入緩沖區。
*/
publi cclas sBufferdReaderTest01{
publicstaticvoidmain(String[]args){
FileReader reader=null;
BufferedReader br=null;
try{
//構造方法,需要傳入一個基本流。
//被傳入的流叫做節點流,傳入的流叫做包裝流。也叫處理流。
//BufferedReader就是包裝流,FileReadr就是節點流。
reader = new FileReader("src/tempfile");
br = new BufferedReader(reader);
String readLine = null;
//帶有的readLine方法可以一次讀取一行,但是不會自動換行。需要使用println()。
while((readLine=br.readLine())!=null){
System.out.println(readLine);
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!=null){
//帶有包裝的流關閉時,只需要關閉高級流即可,里面的流會自動關閉。
try{
br.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
4. 轉換流(字節轉換為字符流)
- 成員
-
InputStreamReader
--字節輸入流轉化為字符輸入流 -
OutPutStreamWriter
--字節輸出流轉化為字符輸出流
-
- 構造方法
- 從構造方法來了,轉換流的最大特點是
可以指定字符的編碼
,可以一定程度解決編碼需求
- 從構造方法來了,轉換流的最大特點是
- 例子
publicstaticvoidmain(String[]args)throwsIOException{
//InputStreamReader把字節流轉換為字符流
FileInputStreamin=newFileInputStream("src/tempfile");
//把字節流轉換為字符流,返回值是一個字符流
//字節流和包裝流是相對的概念,傳入的那個總是相對包裝的那個叫字節流。
InputStreamReaderreader=newInputStreamReader(in);
//通過字符流創建緩沖流
BufferedReaderbr=newBufferedReader(reader);
//合並格式。BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream("src/tempfile")));
Stringline=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
4.數組輸入輸出流
- 包含的類
- ByteArrayInputStream 字節數組輸出流
- ByteArrayOutputStream 字節數組輸入流
- CharArrayReader 字符數組輸入流
- CharArrayWriter 字符數組輸出流
- 構造方法
- 相當於是一個數組,byte[] 或 chsr[]
- 可以一次行把文件寫入(全部或部分)
- 寫的時候是從緩沖區里面拿,減少資源占用
- 使用后不需要關閉--
本質是數組的讀寫
-
- 使用場景
-
可以從網絡上或服務器得到某些資源存入,然后返回給前端該數組。
這些流主要是對數據進行中專作用,僅僅是會自動擴容的容器
,如果想要寫入文件還需要inputStream這樣的流.
//可以讀取網頁的html文件 點擊F12顯示文件
URL url = new URL("https://www.html.cn/qa/other/19304.html");
InputStream in = url.openStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = in.read(buffer)) != -1)
{
output.write(buffer, 0, len);
}
System.err.println(new String(output.toByteArray()));
/**
*可以看到我們輸入輸出流並不需要對應起來
*
*/
public static void main(String[] args) throws IOException {
URL url = new URL("https://www.baidu.com");
InputStream in = url.openStream();
BufferedInputStream bfin = new BufferedInputStream(in);
File file = new File("D:/xsq/test");
System.out.println(file.getAbsolutePath());
if( !file.exists()){
file.mkdirs();
file = new File(file.getAbsolutePath()+"/321blog.txt");
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
int readCount =0;
while((readCount=bfin.read()) != -1){
fos.write(readCount);
}
}