溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JavaSE IO流常用方法有哪些

發布時間:2022-10-17 16:13:40 來源:億速云 閱讀:161 作者:iii 欄目:編程語言

JavaSE IO流常用方法有哪些

目錄

  1. 引言
  2. IO流概述
  3. 字節流
  4. 字符流
  5. 文件操作
  6. 緩沖流
  7. 轉換流
  8. 數據流
  9. 對象流
  10. 打印流
  11. 序列流
  12. 管道流
  13. 標準輸入輸出流
  14. 總結

引言

在Java編程中,IO流(輸入輸出流)是處理數據輸入輸出的重要工具。無論是讀取文件、網絡數據傳輸,還是與用戶交互,IO流都扮演著至關重要的角色。Java提供了豐富的IO流類庫,涵蓋了字節流、字符流、緩沖流、轉換流等多種類型。本文將詳細介紹Java SE中常用的IO流及其方法,幫助開發者更好地理解和應用這些工具。

IO流概述

IO流是Java中用于處理輸入輸出的抽象概念。根據數據的傳輸方向,IO流可以分為輸入流(InputStream/Reader)和輸出流(OutputStream/Writer)。根據數據的處理方式,IO流又可以分為字節流和字符流。

  • 字節流:以字節為單位進行數據傳輸,適用于處理二進制數據,如圖片、音頻、視頻等。
  • 字符流:以字符為單位進行數據傳輸,適用于處理文本數據。

字節流

InputStream

InputStream是所有字節輸入流的基類,提供了讀取字節數據的基本方法。

常用方法

  • int read():讀取一個字節的數據,返回讀取的字節值(0-255),如果到達流的末尾則返回-1。
  • int read(byte[] b):將數據讀入字節數組b,返回實際讀取的字節數。
  • int read(byte[] b, int off, int len):將數據讀入字節數組b的指定位置,從off開始,最多讀取len個字節。
  • long skip(long n):跳過n個字節的數據,返回實際跳過的字節數。
  • int available():返回當前可讀取的字節數。
  • void close():關閉輸入流,釋放相關資源。

示例代碼

InputStream inputStream = new FileInputStream("example.txt");
int data;
while ((data = inputStream.read()) != -1) {
    System.out.print((char) data);
}
inputStream.close();

OutputStream

OutputStream是所有字節輸出流的基類,提供了寫入字節數據的基本方法。

常用方法

  • void write(int b):寫入一個字節的數據。
  • void write(byte[] b):寫入字節數組b中的所有數據。
  • void write(byte[] b, int off, int len):寫入字節數組b中從off開始的len個字節。
  • void flush():刷新輸出流,強制將緩沖區中的數據寫入目標。
  • void close():關閉輸出流,釋放相關資源。

示例代碼

OutputStream outputStream = new FileOutputStream("example.txt");
byte[] data = "Hello, World!".getBytes();
outputStream.write(data);
outputStream.close();

字符流

Reader

Reader是所有字符輸入流的基類,提供了讀取字符數據的基本方法。

常用方法

  • int read():讀取一個字符的數據,返回讀取的字符值(0-65535),如果到達流的末尾則返回-1。
  • int read(char[] cbuf):將數據讀入字符數組cbuf,返回實際讀取的字符數。
  • int read(char[] cbuf, int off, int len):將數據讀入字符數組cbuf的指定位置,從off開始,最多讀取len個字符。
  • long skip(long n):跳過n個字符的數據,返回實際跳過的字符數。
  • boolean ready():判斷流是否準備好被讀取。
  • void close():關閉輸入流,釋放相關資源。

示例代碼

Reader reader = new FileReader("example.txt");
int data;
while ((data = reader.read()) != -1) {
    System.out.print((char) data);
}
reader.close();

Writer

Writer是所有字符輸出流的基類,提供了寫入字符數據的基本方法。

常用方法

  • void write(int c):寫入一個字符的數據。
  • void write(char[] cbuf):寫入字符數組cbuf中的所有數據。
  • void write(char[] cbuf, int off, int len):寫入字符數組cbuf中從off開始的len個字符。
  • void write(String str):寫入字符串str。
  • void write(String str, int off, int len):寫入字符串str中從off開始的len個字符。
  • void flush():刷新輸出流,強制將緩沖區中的數據寫入目標。
  • void close():關閉輸出流,釋放相關資源。

示例代碼

Writer writer = new FileWriter("example.txt");
writer.write("Hello, World!");
writer.close();

文件操作

File類

File類用于表示文件和目錄路徑名的抽象表示,提供了文件和目錄的操作方法。

常用方法

  • boolean exists():判斷文件或目錄是否存在。
  • boolean isFile():判斷是否是一個文件。
  • boolean isDirectory():判斷是否是一個目錄。
  • String getName():獲取文件或目錄的名稱。
  • String getPath():獲取文件或目錄的路徑。
  • long length():獲取文件的長度(字節數)。
  • boolean createNewFile():創建一個新的空文件。
  • boolean delete():刪除文件或目錄。
  • boolean mkdir():創建一個目錄。
  • boolean mkdirs():創建一個目錄,包括所有必需的父目錄。
  • String[] list():獲取目錄中的文件和子目錄的名稱數組。
  • File[] listFiles():獲取目錄中的文件和子目錄的File對象數組。

示例代碼

File file = new File("example.txt");
if (file.exists()) {
    System.out.println("File exists: " + file.getName());
} else {
    System.out.println("File does not exist.");
}

RandomAccessFile

RandomAccessFile類允許隨機訪問文件內容,既可以讀取也可以寫入。

常用方法

  • long getFilePointer():獲取當前文件指針的位置。
  • void seek(long pos):設置文件指針的位置。
  • int read():讀取一個字節的數據。
  • int read(byte[] b):將數據讀入字節數組b。
  • int read(byte[] b, int off, int len):將數據讀入字節數組b的指定位置。
  • void write(int b):寫入一個字節的數據。
  • void write(byte[] b):寫入字節數組b中的所有數據。
  • void write(byte[] b, int off, int len):寫入字節數組b中從off開始的len個字節。
  • void close():關閉文件,釋放相關資源。

示例代碼

RandomAccessFile raf = new RandomAccessFile("example.txt", "rw");
raf.seek(raf.length());
raf.write("Hello, World!".getBytes());
raf.close();

緩沖流

BufferedInputStream

BufferedInputStreamInputStream提供了緩沖功能,可以提高讀取效率。

常用方法

  • int read():讀取一個字節的數據。
  • int read(byte[] b):將數據讀入字節數組b。
  • int read(byte[] b, int off, int len):將數據讀入字節數組b的指定位置。
  • long skip(long n):跳過n個字節的數據。
  • int available():返回當前可讀取的字節數。
  • void close():關閉輸入流,釋放相關資源。

示例代碼

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("example.txt"));
int data;
while ((data = bis.read()) != -1) {
    System.out.print((char) data);
}
bis.close();

BufferedOutputStream

BufferedOutputStreamOutputStream提供了緩沖功能,可以提高寫入效率。

常用方法

  • void write(int b):寫入一個字節的數據。
  • void write(byte[] b):寫入字節數組b中的所有數據。
  • void write(byte[] b, int off, int len):寫入字節數組b中從off開始的len個字節。
  • void flush():刷新輸出流,強制將緩沖區中的數據寫入目標。
  • void close():關閉輸出流,釋放相關資源。

示例代碼

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("example.txt"));
byte[] data = "Hello, World!".getBytes();
bos.write(data);
bos.close();

BufferedReader

BufferedReaderReader提供了緩沖功能,可以提高讀取效率。

常用方法

  • int read():讀取一個字符的數據。
  • int read(char[] cbuf):將數據讀入字符數組cbuf。
  • int read(char[] cbuf, int off, int len):將數據讀入字符數組cbuf的指定位置。
  • String readLine():讀取一行文本。
  • long skip(long n):跳過n個字符的數據。
  • boolean ready():判斷流是否準備好被讀取。
  • void close():關閉輸入流,釋放相關資源。

示例代碼

BufferedReader br = new BufferedReader(new FileReader("example.txt"));
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}
br.close();

BufferedWriter

BufferedWriterWriter提供了緩沖功能,可以提高寫入效率。

常用方法

  • void write(int c):寫入一個字符的數據。
  • void write(char[] cbuf):寫入字符數組cbuf中的所有數據。
  • void write(char[] cbuf, int off, int len):寫入字符數組cbuf中從off開始的len個字符。
  • void write(String str):寫入字符串str。
  • void write(String str, int off, int len):寫入字符串str中從off開始的len個字符。
  • void newLine():寫入一個行分隔符。
  • void flush():刷新輸出流,強制將緩沖區中的數據寫入目標。
  • void close():關閉輸出流,釋放相關資源。

示例代碼

BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"));
bw.write("Hello, World!");
bw.newLine();
bw.write("This is a new line.");
bw.close();

轉換流

InputStreamReader

InputStreamReader是字節流到字符流的橋梁,可以將字節流轉換為字符流。

常用方法

  • int read():讀取一個字符的數據。
  • int read(char[] cbuf):將數據讀入字符數組cbuf。
  • int read(char[] cbuf, int off, int len):將數據讀入字符數組cbuf的指定位置。
  • boolean ready():判斷流是否準備好被讀取。
  • void close():關閉輸入流,釋放相關資源。

示例代碼

InputStreamReader isr = new InputStreamReader(new FileInputStream("example.txt"));
int data;
while ((data = isr.read()) != -1) {
    System.out.print((char) data);
}
isr.close();

OutputStreamWriter

OutputStreamWriter是字符流到字節流的橋梁,可以將字符流轉換為字節流。

常用方法

  • void write(int c):寫入一個字符的數據。
  • void write(char[] cbuf):寫入字符數組cbuf中的所有數據。
  • void write(char[] cbuf, int off, int len):寫入字符數組cbuf中從off開始的len個字符。
  • void write(String str):寫入字符串str。
  • void write(String str, int off, int len):寫入字符串str中從off開始的len個字符。
  • void flush():刷新輸出流,強制將緩沖區中的數據寫入目標。
  • void close():關閉輸出流,釋放相關資源。

示例代碼

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("example.txt"));
osw.write("Hello, World!");
osw.close();

數據流

DataInputStream

DataInputStream允許應用程序以與機器無關的方式從底層輸入流中讀取基本Java數據類型。

常用方法

  • int read(byte[] b):將數據讀入字節數組b。
  • int read(byte[] b, int off, int len):將數據讀入字節數組b的指定位置。
  • boolean readBoolean():讀取一個boolean值。
  • byte readByte():讀取一個byte值。
  • char readChar():讀取一個char值。
  • double readDouble():讀取一個double值。
  • float readFloat():讀取一個float值。
  • int readInt():讀取一個int值。
  • long readLong():讀取一個long值。
  • short readShort():讀取一個short值。
  • String readUTF():讀取一個UTF-8編碼的字符串。
  • void close():關閉輸入流,釋放相關資源。

示例代碼

DataInputStream dis = new DataInputStream(new FileInputStream("example.dat"));
int intValue = dis.readInt();
double doubleValue = dis.readDouble();
String stringValue = dis.readUTF();
dis.close();

DataOutputStream

DataOutputStream允許應用程序以與機器無關的方式向底層輸出流中寫入基本Java數據類型。

常用方法

  • void write(byte[] b):寫入字節數組b中的所有數據。
  • void write(byte[] b, int off, int len):寫入字節數組b中從off開始的len個字節。
  • void writeBoolean(boolean v):寫入一個boolean值。
  • void writeByte(int v):寫入一個byte值。
  • void writeChar(int v):寫入一個char值。
  • void writeDouble(double v):寫入一個double值。
  • void writeFloat(float v):寫入一個float值。
  • void writeInt(int v):寫入一個int值。
  • void writeLong(long v):寫入一個long值。
  • void writeShort(int v):寫入一個short值。
  • void writeUTF(String str):寫入一個UTF-8編碼的字符串。
  • void flush():刷新輸出流,強制將緩沖區中的數據寫入目標。
  • void close():關閉輸出流,釋放相關資源。

示例代碼

DataOutputStream dos = new DataOutputStream(new FileOutputStream("example.dat"));
dos.writeInt(123);
dos.writeDouble(123.456);
dos.writeUTF("Hello, World!");
dos.close();

對象流

ObjectInputStream

ObjectInputStream用于從輸入流中讀取對象。

常用方法

  • Object readObject():讀取一個對象。
  • int read():讀取一個字節的數據。
  • int read(byte[] b):將數據讀入字節數組b。
  • int read(byte[] b, int off, int len):將數據讀入字節數組b的指定位置。
  • void close():關閉輸入流,釋放相關資源。

示例代碼

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("example.dat"));
Object obj = ois.readObject();
ois.close();

ObjectOutputStream

ObjectOutputStream用于將對象寫入輸出流。

常用方法

  • void writeObject(Object obj):寫入一個對象。
  • void write(int b):寫入一個字節的數據。
  • void write(byte[] b):寫入字節數組b中的所有數據。
  • void write(byte[] b, int off, int len):寫入字節數組b中從off開始的len個字節。
  • void flush():刷新輸出流,強制將緩沖區中的數據寫入目標。
  • void close():關閉輸出流,釋放相關資源。

示例代碼

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("example.dat"));
oos.writeObject(new MyClass());
oos.close();

打印流

PrintStream

PrintStream為其他輸出流添加了功能,使它們能夠方便地打印各種數據值表示形式。

常用方法

  • void print(boolean b):打印一個boolean值。
  • void print(char c):打印一個char值。
  • void print(int i):打印一個int值。
  • void print(long l):打印一個long值。
  • void print(float f):打印一個float值。
  • void print(double d):打印一個double值。
  • void print(char[] s):打印一個字符數組。
  • void print(String s):打印一個字符串。
  • void print(Object obj):打印一個對象。
  • void println()
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女