一、什麼是IO流?
IO(Input/Output)即輸入與輸出,Java中的IO流是用來處理文件與數據流的機制。Java將流定義為一個連續的字符序列和位元組序列。在Java中,流是用於讀寫操作的,數據流向被讀取,也可以流向被寫入。IO流通常分為位元組流和字符流。位元組流用於處理二進制數據,例如處理圖像和音頻文件;字符流則用於處理文本數據,例如處理文本文件。Java中的IO流主要有三個途徑:File類、流式傳輸API、NIO。
public class IOStreamDemo {
public static void main(String[] args) {
try {
File file = new File("test.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
isr.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、JAVA IO流的分類
Java的IO流可以根據數據傳輸方式劃分為位元組流和字符流,可以根據操作的數據單位劃分為節點流和處理流。
1. 位元組流和字符流
位元組流主要由InputStream和OutputStream構成,其中InputStream是抽象類,可以根據具體數據類型採取相應的輸入流,如FileInputStream;OutputStream也是抽象類,可以根據具體數據類型採取相應的輸出流,如FileOutputStream。
字符流主要由Reader和Writer構成,其中Reader是抽象類,可以根據具體數據類型採取相應的讀取字符流,如FileReader;Writer也是抽象類,可以根據具體數據類型採取相應的寫入字符流,如FileWriter。
import java.io.*;
public class ByteStreamDemo {
public static void main(String[] args) {
try {
File file = new File("test.txt");
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
System.out.println(new String(b,0,len, "UTF-8"));
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 節點流和處理流
節點流負責數據的讀寫,處理流則利用節點流進行數據讀寫的過程中添加相應的處理方式。節點流可以直接操作文件,但是處理流不能單獨存在,它必須依託於其他的節點流的基礎之上。常見的例子是BufferedReader和文件的結合。
import java.io.*;
public class ReaderDemo {
public static void main(String[] args) {
try {
File file = new File("test.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
isr.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、Java IO流的常用類
1. FileInputStream 和 FileOutputStream類
FileInputStream和FileOutputStream用於讀取和輸出文件數據。這兩個類是位元組流的標準類型,它們通過簡單的InputStream和OutputStream類進行包裝實現。
import java.io.*;
public class FileStreamDemo {
public static void main(String[] args) {
File file = new File("test.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
String data = "這是Java IO流的使用指南";
byte[] b = data.getBytes("UTF-8");
fos.write(b);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[(int) file.length()];
fis.read(b);
System.out.println(new String(b, "UTF-8"));
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. Reader和Writer類
Reader和Writer類是用於讀取和輸出字符數據的類,它提供了直觀的讀寫接口,適合於處理文本類型數據。
import java.io.*;
public class FileWriterDemo {
public static void main(String[] args) {
File file = new File("test.txt");
try {
FileWriter fw = new FileWriter(file);
fw.write("這是Java IO流的使用指南");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader fr = new FileReader(file);
char[] b = new char[(int) file.length()];
fr.read(b);
System.out.println(new String(b));
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. BufferedReader 和 BufferedWriter類
BufferedReader和BufferedWriter是高效的讀取和輸出字符數據的類,它們提供了緩衝讀寫接口,可以提高IO效率。
import java.io.*;
public class BufferedWriterDemo {
public static void main(String[] args) {
File file = new File("test.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write("這是Java IO流的使用指南");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、Java NIO介紹
NIO(New IO)是JDK1.4版本引入的一組帶有新功能的IO API。NIO支持面向緩衝的,基於通道的IO操作。 NIO是一種基於緩存的IO方式,主要是為了解決BIO(Blocking IO)多線程讀取數據的問題,在NIO中可以設置一個或多個緩存裝滿需要傳輸的內容,然後再操作緩存中的內容。
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.nio.file.*;
public class NIODemo {
public static void main(String[] args) {
try {
Path path = Paths.get("test.txt");
String input = "這是Java NIO的使用指南";
Files.write(path, input.getBytes(), StandardOpenOption.CREATE);
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
Charset charset = Charset.forName("UTF-8");
FileChannel fileChannel = FileChannel.open(path);
int bytesRead = fileChannel.read(byteBuffer);
while (bytesRead != -1){
byteBuffer.flip();
while(byteBuffer.hasRemaining()) {
System.out.print(charset.decode(byteBuffer));
}
byteBuffer.clear();
bytesRead = fileChannel.read(byteBuffer);
}
fileChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/239278.html
微信掃一掃
支付寶掃一掃