Java 中的 IO 流(输入/输出流),主要包括以下几种基本类型:
-
字节输入/输出流:
- 输入流:
InputStream
及其子类 - 输出流:
OutputStream
及其子类
- 输入流:
-
字符输入/输出流:
- 输入流:
Reader
及其子类 - 输出流:
Writer
及其子类
- 输入流:
-
装饰者流:用于增强功能,如缓冲、处理等。
- 缓冲流:
BufferedInputStream
,BufferedOutputStream
,BufferedReader
,BufferedWriter
- 处理流:
DataInputStream
,DataOutputStream
,FileReader
,FileWriter
等
- 缓冲流:
-
转换流:
InputStreamReader
:将字节输入流转换为字符输入流OutputStreamWriter
:将字符输出流转换为字节输出流
-
文件相关流:
FileInputStream
和FileOutputStream
FileReader
和FileWriter
-
其他流:
ByteArrayInputStream
和ByteArrayOutputStream
PipedInputStream
和PipedOutputStream
SequenceInputStream
PrintStream
和PrintWriter
RandomAccessFile
示例
1. 字节流示例:文件复制
使用 FileInputStream
和 FileOutputStream
复制文件:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class FileCopy {public static void main(String[] args) {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("source.txt");fos = new FileOutputStream("destination.txt");byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = fis.read(buffer)) != -1) {fos.write(buffer, 0, bytesRead);}} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null) fis.close();if (fos != null) fos.close();} catch (IOException e) {e.printStackTrace();}}}
}
2. 字符流示例:文件内容读取和写入
使用 FileReader
和 FileWriter
:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class FileReadWrite {public static void main(String[] args) {FileReader fr = null;FileWriter fw = null;try {fr = new FileReader("source.txt");fw = new FileWriter("destination.txt");char[] buffer = new char[1024];int charsRead;while ((charsRead = fr.read(buffer)) != -1) {fw.write(buffer, 0, charsRead);}} catch (IOException e) {e.printStackTrace();} finally {try {if (fr != null) fr.close();if (fw != null) fw.close();} catch (IOException e) {e.printStackTrace();}}}
}
3. 缓冲流示例:高效文件复制
使用 BufferedInputStream
和 BufferedOutputStream
:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class BufferedFileCopy {public static void main(String[] args) {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(new FileInputStream("source.txt"));bos = new BufferedOutputStream(new FileOutputStream("destination.txt"));byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = bis.read(buffer)) != -1) {bos.write(buffer, 0, bytesRead);}} catch (IOException e) {e.printStackTrace();} finally {try {if (bis != null) bis.close();if (bos != null) bos.close();} catch (IOException e) {e.printStackTrace();}}}
}
4. 转换流示例:字节流转换为字符流
使用 InputStreamReader
和 OutputStreamWriter
:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;public class ConvertStreamExample {public static void main(String[] args) {FileInputStream fis = null;FileOutputStream fos = null;InputStreamReader isr = null;OutputStreamWriter osw = null;try {fis = new FileInputStream("source.txt");fos = new FileOutputStream("destination.txt");isr = new InputStreamReader(fis, "UTF-8");osw = new OutputStreamWriter(fos, "UTF-8");char[] buffer = new char[1024];int charsRead;while ((charsRead = isr.read(buffer)) != -1) {osw.write(buffer, 0, charsRead);}} catch (IOException e) {e.printStackTrace();} finally {try {if (isr != null) isr.close();if (osw != null) osw.close();if (fis != null) fis.close();if (fos != null) fos.close();} catch (IOException e) {e.printStackTrace();}}}
}
基于 Java IO 流的不同用法,帮助大家在实际应用中选择和使用合适的IO流。