成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一文搞定Java I/O流,看看這些你就知道了!

開發 后端
很多朋友問我,如何才能學好IO流,對各種流的概念,云里霧里的,不求甚解。用到的時候,現百度,功能雖然實現了,但是為什么用這個?今天,我就用一天的時間,整理一下關于Java I/O流的知識點,分享給大家。

大家好,我是哪吒。

很多朋友問我,如何才能學好IO流,對各種流的概念,云里霧里的,不求甚解。用到的時候,現百度,功能雖然實現了,但是為什么用這個?不知道。更別說效率問題了~

下次再遇到,再百度,“良性循環”。

今天,我就用一天的時間,整理一下關于Java I/O流的知識點,分享給大家。

每一種IO流,都配有示例代碼,大家可以跟著敲一遍,找找感覺~

圖片

一、InputStream

InputStream 代表一個輸入流,它是一個抽象類,不能被實例化。InputStream 定義了一些通用方法,如 read() 和 skip() 等,用于從輸入流中讀取數據。常用的 InputStream 實現類包括:

1、FileInputStream的代碼示例

下面是使用 FileInputStream 讀取文件內容的示例代碼:

import java.io.*;

public class FileInputStreamExample {

    public static void main(String[] args) {
        // 要讀取的文件路徑和名稱
        String filePath = "C:/example/file.txt";
        // 創建輸入流對象
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(filePath);
            byte[] buffer = new byte[1024];
            int len;
            // 使用 while 循環讀取文件,每次最多讀取 1024 個字節
            while ((len = fis.read(buffer)) != -1) {
                // 將讀取的字節轉換為字符串,并輸出到控制臺
                String content = new String(buffer, 0, len, "UTF-8");
                System.out.println(content);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + filePath);
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        } finally {
            // 關閉輸入流
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing file: " + e.getMessage());
            }
        }
    }
}

示例代碼說明:

  • 在示例中,我們首先指定要讀取的文件路徑和名稱。在實際使用中,您應該將其替換為實際的文件路徑和名稱。
  • 然后,我們使用文件路徑創建一個FileInputStream對象。注意,在創建FileInputStream對象時,我們必須提供要讀取的文件的路徑和名稱。
  • 接著,我們使用while循環從FileInputStream對象中讀取數據。每次循環中,我們使用read()方法讀取最多1024字節,并將讀取的字節存儲在一個緩沖區中。然后,我們將讀取的字節轉換為字符串,并在控制臺上輸出。在while循環結束后,我們關閉FileInputStream對象。
  • 注意:在使用FileInputStream類時,我們需要確保文件存在,并且我們有讀取文件的權限。此外,在實際應用中,可能需要根據需要使用更高效的方法讀取大型文件,以避免IO開銷的問題。

2、ByteArrayInputStream的代碼示例

下面是使用 ByteArrayInputStream 讀取字節數組內容的示例代碼:

import java.io.*;

public class ByteArrayInputStreamExample {

    public static void main(String[] args) {
        byte[] bytes = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 };
        // 創建字節輸入流對象
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

        try {
            byte[] buffer = new byte[1024];
            int len;
            // 使用 while 循環讀取字節數組中的內容,每次最多讀取 1024 個字節
            while ((len = bais.read(buffer)) != -1) {
                // 將讀取的字節轉換為字符串,并輸出到控制臺
                String content = new String(buffer, 0, len, "UTF-8");
                System.out.println(content);
            }
        } catch (IOException e) {
            System.out.println("Error reading byte array: " + e.getMessage());
        } finally {
            // 關閉輸入流
            try {
                if (bais != null) {
                    bais.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing byte array input stream: " + e.getMessage());
            }
        }
    }
}

示例代碼說明:

  • 在示例中,我們首先創建了一個字節數組,其中包含ASCII碼為72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33的字符序列"Hello World!"。
  • 然后,我們使用字節數組創建了一個ByteArrayInputStream對象。注意,在創建ByteArrayInputStream對象時,我們必須提供要從中讀取的字節數組。
  • 接著,我們使用while循環從ByteArrayInputStream對象中讀取數據。每次循環中,我們使用read()方法讀取最多1024字節,并將讀取的字節存儲在一個緩沖區中。然后,我們將讀取的字節轉換為字符串,并在控制臺上輸出。在while循環結束后,我們關閉ByteArrayInputStream對象。
  • 注意:在使用ByteArrayInputStream類時,字節數組是一次性全部加載到內存中的,如果字節數組較大,可能會導致內存不足的問題。此外,在實際應用中,可能需要使用更高效的數據源(如文件或網絡流)來存儲數據,以避免內存限制的問題。

3、PipedInputStream的代碼示例

PipedInputStream:管道輸入流,用于線程之間的通信。

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedInputStreamExample {

    public static void main(String[] args) throws Exception {
        
        // 創建一對PipedInputStream和PipedOutputStream
        PipedInputStream input = new PipedInputStream();
        PipedOutputStream output = new PipedOutputStream(input);

        // 創建一個寫線程
        Thread writerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 寫入一些數據到PipedOutputStream
                    output.write("Hello, World!".getBytes());
                    output.close(); // 關閉PipedOutputStream
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        // 創建一個讀線程
        Thread readerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 讀取PipedInputStream中的數據
                    int data;
                    while ((data = input.read()) != -1) {
                        System.out.print((char) data); // 將數據打印到控制臺
                    }
                    input.close(); // 關閉PipedInputStream
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        // 啟動寫線程和讀線程
        writerThread.start();
        readerThread.start();

        // 等待寫線程和讀線程完成
        writerThread.join();
        readerThread.join();
    }
}

代碼說明

PipedInputStream和PipedOutputStream是Java IO庫提供的一對管道流,可以用于數據的發送和接收。

  • 在示例中,我們首先創建了一個PipedInputStream和一個PipedOutputStream。它們被連接在一起,使得我們可以像使用一個普通的輸入流- 和輸出流一樣進行讀寫操作。
  • 接著,我們創建一個寫線程來向管道中寫入數據。在這個例子中,我們寫入了字符串"Hello, World!"。
  • 然后,我們創建一個讀線程來從管道中讀取數據。我們使用了一個while循環來讀取數據,直到遇到了流的末尾。
  • 最后,我們啟動寫線程和讀線程,等待它們完成,然后關閉流。
  • 注意:如果讀線程在數據被寫入管道之前就開始讀取流,它將會阻塞(即等待數據被寫入)。
  • 另外,還要注意線程之間的同步問題。在這個例子中,我們使用了一個簡單的join()方法來等待寫線程和讀線程完成。在實際使用中,您可能需要使用更高級的同步機制來確保線程之間的正確協作。

二、 OutputStream

OutputStream 代表一個輸出流,它也是一個抽象類,不能被實例化。OutputStream 定義了一些通用方法,如 write() 和 flush() 等,用于向輸出流中寫入數據。常用的 OutputStream 實現類包括:

1、FileOutputStream代碼示例

文件輸出流,用于向文件中寫入數據。

import java.io.*;

public class FileOutputStreamExample {

    public static void main(String[] args) {
        // 要寫入的文件路徑和名稱
        String filePath = "C:/example/output.txt";
        // 要寫入文件的內容
        String content = "Hello, World!";
        // 創建輸出流對象
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(filePath);
            // 將字符串轉換為字節數組,并將其寫入文件
            fos.write(content.getBytes("UTF-8"));
            // 刷新輸出流
            fos.flush();
            // 輸出提示信息
            System.out.println("Content has been written to " + filePath);
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + filePath);
        } catch (IOException e) {
            System.out.println("Error writing file: " + e.getMessage());
        } finally {
            // 關閉輸出流
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing file: " + e.getMessage());
            }
        }
    }
}

示例代碼說明:

  • 在示例中,我們首先指定要寫入的文件路徑和名稱。在實際使用中,您應該將其替換為實際的文件路徑和名稱。
  • 然后,我們創建一個用于寫入文件的輸出流對象。在創建FileOutputStream對象時,如果文件不存在,Java將會自動創建它。
  • 接著,我們將要寫入的內容轉換為字節數組,并使用write()方法將其寫入文件。
  • 然后,我們使用flush()方法刷新輸出流。在文件操作完成后,我們應該始終調用flush()方法以確保所有數據都被寫入到磁盤上的文件中。
  • 最后,我們關閉FileOutputStream對象,即使在發生異常時也應該關閉。
  • 注意:在使用FileOutputStream類時,我們需要確保文件存在,并且我們有寫入文件的權限。此外,在實際應用中,可能需要使用更高效的方法來寫入大型文件,以避免IO開銷的問題。

2、ByteArrayOutputStream代碼示例:

字節數組輸出流,用于將數據寫入內存中的字節數組中。

import java.io.*;

public class ByteArrayOutputStreamExample {

    public static void main(String[] args) {
        // 創建字節數組輸出流對象
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            // 將字符串轉換為字節數組,并寫入到字節數組輸出流中
            baos.write("Hello, World!".getBytes("UTF-8"));
            // 將字節數組輸出流中的數據轉換為字節數組
            byte[] bytes = baos.toByteArray();
            // 將字節數組轉換為字符串,并輸出到控制臺
            String content = new String(bytes, "UTF-8");
            System.out.println(content);
        } catch (IOException e) {
            System.out.println("Error writing to byte array: " + e.getMessage());
        } finally {
            // 關閉字節數組輸出流
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                System.out.println("Error closing byte array output stream: " + e.getMessage());
            }
        }
    }
}

示例代碼說明:

  • 在示例中,我們首先創建了一個ByteArrayOutputStream對象,用于向內存中的字節數組中寫入數據。
  • 然后,我們將要寫入的內容轉換為字節數組,并使用write()方法將其寫入ByteArrayOutputStream對象。
  • 接著,我們調用toByteArray()方法將ByteArrayOutputStream對象中的數據轉換為字節數組。需要注意的是,要在調用toByteArray()方法之前先關閉ByteArrayOutputStream。
  • 最后,我們將字節數組轉換為字符串,并將其輸出到控制臺。
  • 注意:在使用ByteArrayOutputStream類時,需要注意內存占用問題。BytesArrayOutputStream類主要用于在內存中臨時存儲數據。對于大數據,可能需要使用其他方式存儲。

3、PipedOutputStream代碼示例:

管道輸出流,用于線程之間的通信。

import java.io.*;

public class PipedOutputStreamExample {

    public static void main(String[] args) {
        // 創建一對PipedInputStream和PipedOutputStream
        PipedInputStream input = new PipedInputStream();
        PipedOutputStream output = new PipedOutputStream();

        try {
            // 將輸入流和輸出流連接起來
            input.connect(output);

            // 創建一個寫線程
            Thread writerThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 寫入一些數據到PipedOutputStream
                        output.write("Hello, World!".getBytes("UTF-8"));
                        // 刷新PipedOutputStream
                        output.flush();
                        // 關閉PipedOutputStream
                        output.close();
                    } catch (IOException e) {
                        System.out.println("Error writing to pipe: " + e.getMessage());
                    }
                }
            });

            // 創建一個讀線程
            Thread readerThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        // 讀取PipedInputStream中的數據
                        byte[] buffer = new byte[1024];
                        int len = input.read(buffer);
                        // 將讀取的字節轉換為字符串,并輸出到控制臺
                        String content = new String(buffer, 0, len, "UTF-8");
                        System.out.println(content);
                        // 關閉PipedInputStream
                        input.close();
                    } catch (IOException e) {
                        System.out.println("Error reading from pipe: " + e.getMessage());
                    }
                }
            });

            // 啟動寫線程和讀線程
            writerThread.start();
            readerThread.start();

            // 等待寫線程和讀線程完成
            writerThread.join();
            readerThread.join();
        } catch (IOException | InterruptedException e) {
            System.out.println("Error communicating between threads: " + e.getMessage());
        }
    }
}

示例代碼說明:

  • 在示例中,我們首先創建了一對PipedInputStream和PipedOutputStream,用于在線程之間進行通信。
  • 接著,我們使用connect()方法將PipedInputStream和PipedOutputStream連接起來。
  • 然后,我們創建一個寫線程和一個讀線程。在寫線程中,我們向PipedOutputStream寫入數據,并使用flush()和close()方法刷新和關閉輸出流。在讀線程中,我們從PipedInputStream讀取數據,并將其轉換為字符串并打印到控制臺。在讀操作完成后,我們關閉輸入流
  • 最后,我們啟動寫線程和讀線程,并等待它們完成。
  • 注意:在使用PipedInputStream和PipedOutputStream類時,需要考慮線程同步問題,以確保在線程之間正確地交換數據。在實際應用中,您可能需要使用更高級的同步機制來確保線程之間的協作。

三、字符輸入流Reader

除了字節流,Java 還提供字符流,字符流類似于字節流,不同之處在于字符流是按字符讀寫數據,而不是按字節。Java 中最基本的字符流是 Reader 和 Writer,它們是基于 InputStream 和 OutputStream 的轉換類,用于完成字節流與字符流之間的轉換。

常用的實現類包括 FileReader 和 InputStreamReader等

1、FileReader 代碼示例

import java.io.FileReader;  // 引入 FileReader 類
import java.io.IOException; // 引入 IOException 類

public class FileReaderExample {
    public static void main(String[] args) {
        // 定義文件路徑
        String filePath = "example.txt";

        try {
            // 創建 FileReader 對象
            FileReader fileReader = new FileReader(filePath);

            // 讀取字符
            int character;
            while ((character = fileReader.read()) != -1) {
                // 打印字符
                System.out.print((char) character);
            }

            // 關閉 FileReader 對象
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、InputStreamReader 代碼示例

import java.io.BufferedReader; // 引入 BufferedReader 類
import java.io.IOException;    // 引入 IOException 類
import java.io.InputStreamReader; // 引入 InputStreamReader 類

public class InputStreamReaderExample {
    public static void main(String[] args) {

        try {
            // 創建 InputStreamReader 對象
            InputStreamReader inputStreamReader = new InputStreamReader(System.in);

            // 創建 BufferedReader 對象
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            // 獲取用戶輸入
            System.out.println("請輸入字符串:");
            String inputString = bufferedReader.readLine();

            // 打印用戶輸入
            System.out.println("您輸入的字符串是:" + inputString);

            // 關閉 BufferedReader 對象
            bufferedReader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

四、字符輸出流Writer

1、FileWriter代碼示例

import java.io.*;

public class FileWriterExample {
    public static void main(String[] args) {
        FileWriter writer = null;

        try {
            writer = new FileWriter("example.txt");
            writer.write("Hello World!");
            writer.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
}

示例代碼說明:

  • 在這個例子中,創建了一個FileWriter對象 writer,并將字符串"Hello World!"寫入文件 "example.txt"。然后,我們使用close()方法關閉寫入器以確保所有的數據都被刷新到磁盤。
  • 注意:在使用FileWriter時,要確保在不再需要它時關閉它以確保所有的字符都被刷新到文件中。如果您使用Java7或更高版本,可以考慮使用try-with-resources語句,這樣您就不需要顯式地關閉寫入器。

2、OutputStreamWriter代碼示例

import java.io.*;

public class OutputStreamWriterExample {
    public static void main(String[] args) {
        FileOutputStream outputStream = null;
        OutputStreamWriter writer = null;

        try {
            outputStream = new FileOutputStream("example.txt");
            writer = new OutputStreamWriter(outputStream, "UTF-8");
            writer.write("Hello World!");
            writer.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            try {
                if (writer != null)
                    writer.close();
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
}

示例代碼說明:

  • 在這個例子中,創建了一個FileOutputStream對象 outputStream 用于寫入文件 "example.txt"。接著,創建了一個OutputStreamWriter對象 writer,它被用于將字符串"Hello World!"寫入到文件中。指定了"UTF-8"作為字符編碼。
  • 最后,使用close()方法關閉寫入器和輸出流以確保所有的數據都被刷新到磁盤上。
  • 注意:在使用OutputStreamWriter時,要確保在不再需要它時關閉它以確保所有的字符都被刷新到文件中。如果您使用Java 7或更高版本,可以考慮使用try-with-resources語句,這樣您就不需要顯式地關閉寫入器和輸出流。

五、緩沖流

BufferedInputStream 和 BufferedOutputStream 是 I/O 包中提供的緩沖輸入輸出流。它們可以提高 I/O 操作的效率,具有較好的緩存機制,能夠減少磁盤操作,縮短文件傳輸時間。使用 BufferedInputStream 和 BufferedOutputStream 進行讀取和寫入時,Java 會自動調整緩沖區的大小,使其能夠適應不同的數據傳輸速度。

緩沖流代碼示例

import java.io.BufferedInputStream;   // 引入 BufferedInputStream 類
import java.io.BufferedOutputStream;  // 引入 BufferedOutputStream 類
import java.io.FileInputStream;        // 引入 FileInputStream 類
import java.io.FileOutputStream;       // 引入 FileOutputStream 類
import java.io.IOException;            // 引入 IOException 類

public class BufferedStreamsExample {
    public static void main(String[] args) {

        String sourceFile = "source.txt";
        String targetFile = "target.txt";

        try {
            // 創建 BufferedInputStream 和 BufferedOutputStream 對象
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile));

            // 讀取數據,直到讀取的內容為-1
            int data;
            while ((data = bufferedInputStream.read()) != -1) {
                bufferedOutputStream.write(data);
            }

            // 關閉 BufferedInputStream 和 BufferedOutputStream 對象
            bufferedInputStream.close();
            bufferedOutputStream.close();

            // 打印成功信息
            System.out.println("復制文件成功!");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

六、對象流

可以讀取或寫入 Java 對象的流,比較典型的對象流包括ObjectInputStream 和 ObjectOutputStream。

對象流需要將對象序列化和反序列化為字節序列,使用 ObjectInputStream 和 ObjectOutputStream 可以將 Java 對象轉換為字節流進行傳輸或存儲。

在網絡傳輸和文件存儲中,ObjectInputStream 和 ObjectOutputStream 通常會被使用到。

對象流代碼示例

import java.io.FileInputStream;          // 引入 FileInputStream 類
import java.io.FileOutputStream;         // 引入 FileOutputStream 類
import java.io.ObjectInputStream;        // 引入 ObjectInputStream 類
import java.io.ObjectOutputStream;       // 引入 ObjectOutputStream 類
import java.io.Serializable;             // 引入 Serializable 接口

class Person implements Serializable {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return "姓名:" + name + "\n年齡:" + age;
    }
}

public class ObjectStreamsExample {
    public static void main(String[] args) {

        String filePath = "person.dat";

        // 創建 Person 對象
        Person person = new Person("Alice", 20);

        try {
            // 創建 ObjectOutputStream 對象
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(filePath));

            // 將 Person 對象寫入文件
            objectOutputStream.writeObject(person);

            // 關閉 ObjectOutputStream 對象
            objectOutputStream.close();

            // 創建 ObjectInputStream 對象
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));

            // 從文件中讀取 Person 對象
            Person personFromFile = (Person) objectInputStream.readObject();

            // 關閉 ObjectInputStream 對象
            objectInputStream.close();

            // 打印讀取的對象
            System.out.println(personFromFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

本文轉載自微信公眾號「哪吒編程」,可以通過以下二維碼關注。轉載本文請聯系哪吒編程公眾號。

責任編輯:姜華 來源: 哪吒編程
相關推薦

2023-07-27 08:40:45

PawSQL數據庫

2023-09-28 08:42:56

PyQt6Python語言

2021-08-10 23:09:55

區塊鏈數據技術

2019-08-20 13:45:01

阿里巴巴面試Java

2025-02-03 07:00:00

Java接口工具

2019-06-05 15:20:00

MongoDBNoSQL數據庫

2023-12-26 12:18:02

Java設計開發

2019-12-25 10:45:30

Java悲觀鎖

2019-12-19 17:00:01

Java線程

2021-11-27 12:08:49

網絡攻擊微軟網絡安全

2023-08-01 08:27:15

Java I/ONIO

2019-12-02 08:27:43

Dubbo高并發分布式

2020-07-20 10:20:30

this前端代碼

2025-03-07 10:14:03

2019-09-23 10:51:14

JavaJava虛擬機Linux

2017-12-13 12:30:33

LinuxUnix文件系統

2018-12-10 08:47:22

程序員年終獎阿里巴巴

2018-04-02 08:59:33

2018-03-13 11:09:16

屏幕刷新率電腦

2022-07-01 13:38:48

霧計算邊緣計算
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产乡下妇女做爰 | 久久精品国产一区二区三区不卡 | 国产剧情一区 | 亚洲精品视频免费观看 | av在线一区二区三区 | 日韩欧美国产精品 | 91精品国产91久久久久久密臀 | 激情视频中文字幕 | 91精品国产一区二区三区香蕉 | 午夜欧美日韩 | 亚洲精品视频网站在线观看 | 亚洲成人99 | 国产精品视频一 | 久久免费高清视频 | 久久久久久国产精品免费免费男同 | h在线观看| 在线视频 亚洲 | 国产成人aⅴ| 国产一区二区在线视频 | 四虎永久免费在线 | 中文字幕视频网 | 精品久久久久久亚洲综合网站 | 精品国产欧美一区二区 | 中文字幕在线免费观看 | 二区亚洲 | 久草视频在线看 | 亚洲另类自拍 | 最新中文字幕在线 | www.色婷婷 | 精品国产一区二区三区日日嗨 | 国产精品久久久久久久久免费樱桃 | 亚洲狠狠 | 日本中文字幕一区 | 99精品观看 | 欧美日韩亚洲系列 | 久久小视频 | 欧美成人不卡 | 97精品国产一区二区三区 | 国产性网| 色又黄又爽网站www久久 | 日韩免费网站 |