Java中的IO流原理和流的分類詳解
Java IO流原理
1)I/O是input/Output的縮寫,I/O技術是非常實用的技術,用于處理數據傳輸。如讀/寫文件,網絡通訊等;
2)Java程序中,對于數據的輸入和輸出操作以“流(stream)”的方式進行;
3)java.io包下提供了各種“流”類和接口,用以獲取不同種類的數據,并通過方法輸入或輸出數據;
4)輸入(input):讀取外部數據(磁盤、光盤等存儲設備的數據到程序(內存)中);
5)輸出(output):將程序(內存)數據輸出到磁盤、光盤等存儲設備中。
流的分類
1)按照操作數據單位不同分為:字節(jié)流(8bit),字符流(按字符)(分別按照字符和字節(jié)進行讀取,如果從操作單位方面看字符流操作效率較高,但是操作二進制文件時字節(jié)流不會出現錯誤,字節(jié)流操作文本文件比較好);
2)按數據流的流向不同分為:輸入流和輸出流;
3)按流的角色不同分為:節(jié)點流,處理流(包裝流);
| (抽象基類) | 字節(jié)流 | 字符流 |
| 輸入流 | 字節(jié)輸入流的頂級父類 InputStream | 字符輸入流的頂級父類 Reader |
| 輸出流 | 字節(jié)輸出流的頂級父類 OutputStream | 字符輸出流的頂級父類 Writer |
- Java的IO流共涉及40多個類,都是從如上4個抽象基類派生的;
- 由這四個基類派生出來的子類名稱以其父類名作為子類名后綴;
InputStream抽象類實現了Closeable接口,Closeable接口上面還有一個AutoCloseable接口;OutputStream抽象類實現了Closeable接口和Flushable接口;Reader抽象類實現了Closeable接口和Readable接口;Writer抽象類實現了Appendable接口、Flushable接口和Closeable接口;在使用時,創(chuàng)建他們的實現子類。



InputStream常用子類
1)FileInputStream:文件輸入流;
2)BufferedInputStream:緩沖字節(jié)輸入流;
3)ObjectInputStream:對象字節(jié)輸入流;
package com.pero.inputStream;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author Pero
* @version 1.0
*/
public class FileInputStream_ {
public static void main(String[] args) {
}
@Test
//讀取文件
public void test(){
//定義文件路徑
String filePath = "d:\\demo\\test\\HelloWorld.txt";
//定義文件輸入流對象名稱
FileInputStream fileInputStream = null;
//定義接收讀取一字節(jié)數據
int readData;
try {
//獲取文件輸入流對象,用于讀取文件
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1){ //如果返回-1表示已經讀取到最后一位
System.out.print((char)readData); //讀取數據為int型,需要轉成字符型char
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally { //必須關閉流,釋放鏈接文件的流,否則造成資源浪費
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void test2(){
String path = "d:\\demo\\test\\HelloWorld.txt";
FileInputStream fileInputStream = null;
byte[] bytes = new byte[8];
int readLength = 0;
try {
fileInputStream = new FileInputStream(path);
//如果讀取正常返回讀取的字節(jié)數
while((readLength = fileInputStream.read(bytes)) != -1){
//第一次讀取時,bytes第一次接收八個字符hello,wo;
// 此時fileInputStream.read(bytes)返回值為8,
//while循環(huán)不退出,輸出hello,wo后,再次進入循環(huán)
// 第二次讀取bytes第二次接收rld!
// 此時fileInputStream.read(bytes)返回值為4,
// (bytes中存儲的為rld!o,wo,其中o,wo為上次讀取的數據,bytes中沒有將其覆蓋)
//但是new String(bytes, 0, readLength)輸出bytes數組的0~3(0~readLength-1)位的字符
//第三次讀取,讀取到數據的最后一位,此時fileInputStream.read(bytes)返回值為-1退出循環(huán)
System.out.print(new String(bytes,0,readLength));
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}OutputStream常用子類
1)FileOutputStream:文件輸出流。
package com.pero.outputStream_;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* @author Pero
* @version 1.0
*/
public class FileOutputStream_ {
public static void main(String[] args) {
}
@Test
public void writeFile(){
String path = "d:\\demo\\test\\next.txt";
FileOutputStream fileOutputStream = null;
try {
//fileOutputStream = new FileOutputStream(path);以此方式創(chuàng)建輸出流
//輸出文件內容會覆蓋原來的內容
//fileOutputStream = new FileOutputStream(path,true);以此方式創(chuàng)建輸出流
//輸出文件內容會添加到原來的內容之后
fileOutputStream = new FileOutputStream(path,true);
//寫入一個字節(jié)
//fileOutputStream.write('H');
//寫入字符串
String str = "Hello,world!";
//使用getBytes,將字符串轉成字節(jié)(byte)數組
fileOutputStream.write(str.getBytes());
//寫入字符串部分數據Hello(0~5-1)
fileOutputStream.write(str.getBytes(),0,5);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}文件拷貝(圖像)
package com.pero.outputStream_;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Pero
* @version 1.0
*/
public class FileCopy {
public static void main(String[] args) {
}
@Test
public void copy(){
//大文件讀取中,邊讀取數據邊輸出存放文件(流的形式)
//將地址所在文件數據輸入到程序
//輸入、輸出路徑
String inputPath = "C:\\Users\\Pero\\Desktop\\4.jpeg";
//將文件數據輸出到指定地址存放
String outputPath = "D:\\demo\\test\\4.jpeg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
byte[] bytes = new byte[1024]; //飽和讀取數量
int readLength = 0;
try {
fileInputStream = new FileInputStream(inputPath);
fileOutputStream = new FileOutputStream(outputPath);
//讀取文件數據
while ((readLength = fileInputStream.read(bytes)) != -1){
//邊讀邊寫,一定要用write(bytes,0,readLength);
//因為最后一次讀寫,不一定把bytes里的存儲數據完全覆蓋,
//最后剩余的空間是倒數第二次傳輸的部分數據,會發(fā)生錯誤
fileOutputStream.write(bytes,0,readLength);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
finally {
try {
if (fileInputStream != null){
fileInputStream.close();
}
if (fileOutputStream != null){
fileOutputStream.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}FileReader類和FileWriter類(字符流,按照字符來操作IO)
FileReader相關方法
1)構造器:new FileReader(File/String)
2)read:每次讀取單個字符,則返回該字符,如果到了文件末尾則返回-1;
3)read(char[]):批量讀取多個字符到數組,返回讀取到的字符數,如果到了文件末尾則返回-1;
相關API:
1)new String(char[] ):將char[]轉換阿成String;
2)new String(char[] ,off ,len):將char[]的指定部分轉換成String。
package com.pero.reader_;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* @author Pero
* @version 1.0
*/
public class FileReader_ {
public static void main(String[] args) {
}
@Test
public void reader(){
//創(chuàng)建路徑
String path = "d:\\demo\\test\\Story.txt";
FileReader fileReader = null;
int data = 0;
//char[] chars = new char[1024];
try {
fileReader = new FileReader(path);
//循環(huán)讀取
while ((data = fileReader.read()) != -1){
System.out.print((char)data);
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (fileReader != null){
fileReader.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
public void reader1(){
String path = "d:\\demo\\test\\Story.txt";
char[] chars = new char[1024];
int length = 0;
FileReader fileReader = null;
try {
fileReader = new FileReader(path);
while ((length = fileReader.read(chars)) != -1){
System.out.print(new String(chars,0,length));
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
if (fileReader != null){
fileReader.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}FileWriter相關方法
1)構造器:new FileWriter(File/String):覆蓋模式,相當于流的指針在首端;
2)構造器:new FileWriter(File/String,true):追加模式,相當于流的指針在尾端;
3)write(int ):寫入單個字符;
4)write(char[]):寫入指定數組;
5)write(char[],off,len):寫入指定數組的指定部分;
6)write(String):寫入整個字符串;
7)write(String,off,len):寫入字符串的指定部分。
相關API:String類中toCharArray:將String轉換成char[]。
注意:FileWriter使用后,必須要關閉(close)或刷新(flush),否則寫入不到指定的文件。
package com.pero.writer_;
import org.junit.jupiter.api.Test;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author Pero
* @version 1.0
*/
public class FileWriter_ {
public static void main(String[] args) {
}
@Test
public void writer(){
String path = "d:\\demo\\test\\note.txt";
FileWriter fileWriter = null;
char[] chars = {'H','e','l','l','o',',','H','o','r','l','d','!'};
try {
fileWriter = new FileWriter(path/*,true*/);
fileWriter.write('h');
fileWriter.write(chars);
fileWriter.write("不經歷風雨,怎能見彩虹");
fileWriter.write("鋤禾日當午,汗滴禾下土",0,5);
fileWriter.write(chars,0,5);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
fileWriter.close();
//fileWriter.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}到此這篇關于Java中的IO流原理和流的分類詳解的文章就介紹到這了,更多相關Java的IO流內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Seata AT模式TransactionHook被刪除探究
這篇文章主要為大家介紹了Seata AT模式TransactionHook被刪除探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Springboot整合Dubbo教程之項目創(chuàng)建和環(huán)境搭建
本篇文章主要介紹了Springboot整合Dubbo教程之項目創(chuàng)建和環(huán)境搭建,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

