Java IO流是Java語言中重要的工具之一,是用來讀寫數據的機制。IO流用於從外部設備(例如磁碟、網路等)進/出讀取數據,也用於將數據輸出到外部設備。Java IO API提供了用於讀取和寫入數據的各種類和介面。IO流操作是Java編程中必須掌握的內容之一。
一、IO流概述
IO流是Java語言中重要的工具之一,是用來讀寫數據的機制。Java IO API提供了許多類和介面來實現IO流操作。在Java中,IO流被分為輸入流和輸出流。輸入流用於從外部設備進讀取數據,輸出流用於將數據輸出到外部設備。
Java IO流主要用於文件處理、網路編程等方面。Java IO流比較靈活,具有豐富的功能。它可以讀取和寫入不同類型的數據,例如字元、位元組等。Java IO流還可以對讀寫數據進行編碼和解碼,來處理不同類型的數據。
二、Java IO流分類
1.位元組流和字元流
根據讀寫的數據類型,Java IO流被分為位元組流和字元流。位元組流主要用於處理二進位數據,如圖像、音頻等;字元流則主要用於處理文本數據,例如UTF-8編碼的文本文件。Java提供了兩套API,分別是位元組流API和字元流API,可以根據實際需要選擇使用。
2.節點流和處理流
根據數據流向,Java IO流被分為節點流和處理流。節點流可以直接從外部設備進行讀寫操作,處理流則基於節點流進行封裝,在節點流的基礎上提供了更高級別的功能。Java IO API提供了諸如緩衝流、對象流、列印流等處理流,可以方便地進行操作。
3.輸入流和輸出流
根據數據流向,Java IO流被分為輸入流和輸出流。輸入流用於從外部設備(例如磁碟、網路等)進讀取數據,輸出流用於將數據輸出到外部設備。Java IO API提供了各種輸入流和輸出流的實現類,可以方便地進行操作。
三、Java IO流實例
1.文件讀取
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
File file = new File("/path/to/file.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) { // 讀取數據
System.out.print((char) content); // 轉換為char輸出
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close(); // 關閉文件輸入流
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
2.文件寫入
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileWriteExample {
public static void main(String[] args) {
File file = new File("/path/to/file.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
String content = "Hello World!";
fos.write(content.getBytes()); // 寫入數據
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close(); // 關閉文件輸出流
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
3.緩衝流讀寫數據
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedStreamExample {
public static void main(String[] args) {
File inputFile = new File("/path/to/input.txt");
File outputFile = new File("/path/to/output.txt");
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new FileReader(inputFile));
writer = new BufferedWriter(new FileWriter(outputFile));
String line;
while ((line = reader.readLine()) != null) { // 讀取數據
writer.write(line); // 寫入數據
writer.newLine(); // 換行
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close(); // 關閉文件輸入流
}
if (writer != null) {
writer.close(); // 關閉文件輸出流
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
4.對象讀寫
import java.io.*;
public class ObjectStreamExample {
public static void main(String[] args) {
FileOutputStream fos = null;
FileInputStream fis = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
fos = new FileOutputStream("/path/to/file.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(new Student("Alice", 18)); // 寫入對象
fis = new FileInputStream("/path/to/file.txt");
ois = new ObjectInputStream(fis);
Student student = (Student) ois.readObject(); // 讀取對象
System.out.println(student.getName() + " " + student.getAge()); // 輸出對象
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
if (oos != null) {
oos.close();
}
if (ois != null) {
ois.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
class Student implements Serializable {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
本文對Java IO流的概念、分類和實例進行了詳細介紹,涵蓋了常見應用場景。作為Java開發者,掌握好IO流操作技巧是非常重要的。希望讀者能夠通過本文更好地理解和掌握Java IO流操作。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/289495.html
微信掃一掃
支付寶掃一掃