AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

一文搞定Java NIO,以及各種奇葩流

來源: 責編: 時間:2023-08-05 11:46:14 4058觀看
導讀大家好,我是哪吒。很多朋友問我,如何才能學好IO流,對各種流的概念,云里霧里的,不求甚解。用到的時候,現百度,功能雖然實現了,但是為什么用這個?不知道。更別說效率問題了~下次再遇到,再百度,“良性循環”。今天,我就用一天的時間,

nxs28資訊網——每日最新資訊28at.com

大家好,我是哪吒。nxs28資訊網——每日最新資訊28at.com

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

下次再遇到,再百度,“良性循環”。nxs28資訊網——每日最新資訊28at.com

今天,我就用一天的時間,整理一下關于Java I/O流的知識點,分享給大家。nxs28資訊網——每日最新資訊28at.com

每一種IO流,都配有示例代碼,大家可以跟著敲一遍,找找感覺~nxs28資訊網——每日最新資訊28at.com

本篇文章介紹Java NIO以及其它的各種奇葩流。nxs28資訊網——每日最新資訊28at.com

Java NIO (New I/O) 是 Java 1.4 引入的,在 Java 7 中又進行了一些增強。NIO 可以提高 I/O 操作的效率,它的核心是通道 (Channel) 和緩沖區 (Buffer)。nxs28資訊網——每日最新資訊28at.com

一、Channel

Channel 是一種新的 I/O 抽象,它與傳統的 InputStream 和 OutputStream 不同,Channel 可以同時進行讀和寫操作,而且可以對其進行更細粒度的控制。Java NIO 中最基本的 Channel 包括:nxs28資訊網——每日最新資訊28at.com

1、FileChannel代碼示例

使用FileChannel從源文件中讀取內容并將其寫入到目標文件。nxs28資訊網——每日最新資訊28at.com

import java.io.FileInputStream;          // 引入 FileInputStream 類import java.io.FileOutputStream;         // 引入 FileOutputStream 類import java.nio.ByteBuffer;              // 引入 ByteBuffer 類import java.nio.channels.FileChannel;    // 引入 FileChannel 類public class FileChannelExample {    public static void main(String[] args) {        String sourceFile = "source.txt";        String targetFile = "target.txt";        try {            // 使用 FileInputStream 和 FileOutputStream 打開源文件和目標文件            FileInputStream fileInputStream = new FileInputStream(sourceFile);            FileOutputStream fileOutputStream = new FileOutputStream(targetFile);            // 獲取 FileChannel 對象            FileChannel sourceChannel = fileInputStream.getChannel();            FileChannel targetChannel = fileOutputStream.getChannel();            // 創建 ByteBuffer 對象            ByteBuffer buffer = ByteBuffer.allocate(1024);            // 從源文件中讀取內容并將其寫入目標文件            while (sourceChannel.read(buffer) != -1) {                buffer.flip();  // 準備寫入(flip buffer)                targetChannel.write(buffer);  // 向目標文件寫入數據                buffer.clear(); // 緩沖區清空(clear buffer)            }            // 關閉所有的 FileChannel、FileInputStream 和 FileOutputStream 對象            sourceChannel.close();            targetChannel.close();            fileInputStream.close();            fileOutputStream.close();            // 打印成功信息            System.out.println("文件復制成功!");        } catch (Exception e) {            e.printStackTrace();        }    }}

2、DatagramChannel代碼示例

用于 UDP 協議的數據讀寫操作。nxs28資訊網——每日最新資訊28at.com

使用DatagramChannel從一個端口讀取數據并將數據發送到另一個端口。nxs28資訊網——每日最新資訊28at.com

import java.io.IOException;                    // 引入 IOException 類import java.InetSocketAddress;             // 引入 InetSocketAddress 類import java.nio.ByteBuffer;                     // 引入 ByteBuffer 類import java.nio.channels.DatagramChannel;       // 引入 DatagramChannel 類public class DatagramChannelExample {    public static void main(String[] args) {        int receivePort = 8888;        int sendPort = 9999;        try {            // 創建 DatagramChannel 對象            DatagramChannel receiveChannel = DatagramChannel.open();            // 綁定接收端口            receiveChannel.socket().bind(new InetSocketAddress(receivePort));            System.out.println("接收端口 " + receivePort + " 正在等待數據...");            // 創建數據緩沖區對象            ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);            // 從 receiveChannel 接收數據            receiveChannel.receive(receiveBuffer);            // 顯示收到的數據            System.out.println("收到的數據是:" + new String(receiveBuffer.array()));            // 關閉 receiveChannel 對象            receiveChannel.close();            // 創建 DatagramChannel 對象            DatagramChannel sendChannel = DatagramChannel.open();            // 創建數據緩沖區對象            ByteBuffer sendBuffer = ByteBuffer.allocate(1024);            // 向數據緩沖區寫入數據            sendBuffer.clear();            sendBuffer.put("Hello World".getBytes());            sendBuffer.flip();            // 發送數據到指定端口            sendChannel.send(sendBuffer, new InetSocketAddress("localhost", sendPort));            System.out.println("數據已發送到端口 " + sendPort);            // 關閉 sendChannel 對象            sendChannel.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

3、SocketChannel 和 ServerSocketChannel代碼示例

用于 TCP 協議的數據讀寫操作。nxs28資訊網——每日最新資訊28at.com

下面是一個簡單的示例,演示如何使用 SocketChannel 和 ServerSocketChannel 進行基本的 TCP 數據讀寫操作。nxs28資訊網——每日最新資訊28at.com

import java.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;public class TCPExample {    public static void main(String[] args) throws Exception {        // 創建 ServerSocketChannel 并綁定端口        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();        serverSocketChannel.socket().bind(new InetSocketAddress(8888));        serverSocketChannel.configureBlocking(false);        // 創建一個 ByteBuffer 用于接收數據        ByteBuffer buf = ByteBuffer.allocate(1024);        // 等待客戶端連接        while (true) {            SocketChannel socketChannel = serverSocketChannel.accept();            if (socketChannel != null) {                // 客戶端已連接,從 SocketChannel 中讀取數據                int bytesRead = socketChannel.read(buf);                while (bytesRead != -1) {                    // 處理讀取到的數據                    System.out.println(new String(buf.array(), 0, bytesRead));                                        // 清空 ByteBuffer,進行下一次讀取                    buf.clear();                    bytesRead = socketChannel.read(buf);                }            }        }    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 創建一個 ServerSocketChannel 并綁定到本地端口 8888,然后將其設置為非阻塞模式。
  • 創建一個 ByteBuffer 用于接收數據。
  • 進入一個死循環,不斷等待客戶端連接。
  • 當客戶端連接時,從 SocketChannel 中讀取數據,并將讀取到的數據打印到控制臺。
  • 清空 ByteBuffer,進行下一次讀取。

需要注意的點:nxs28資訊網——每日最新資訊28at.com

  • 在代碼中每次讀取結束都需要清空 ByteBuffer,否則其 position 屬性不會自動歸零,可能導致數據讀取不正確。
  • 由于使用非阻塞模式,如果調用了 accept() 方法但沒有立即接收到客戶端連接,該方法會返回 null,需要繼續循環等待。
  • 本代碼只演示了從客戶端讀取數據的部分,如果需要向客戶端發送數據需要調用SocketChannel.write()方法

如果想要向客戶端發送數據,可以使用以下代碼:nxs28資訊網——每日最新資訊28at.com

// 創建一個 ByteBuffer 用于發送數據ByteBuffer buf = ByteBuffer.wrap("Hello, world!".getBytes());// 向客戶端發送數據socketChannel.write(buf);

二、Buffer

Buffer 是一個對象,它包含一些要寫入或要讀出的數據。在 NIO 中,Buffer 可以被看作為一個字節數組,但是它的讀取和寫入操作比直接的字節數組更加高效。NIO 中最常用的 Buffer 類型包括:nxs28資訊網——每日最新資訊28at.com

1、ByteBuffer示例代碼

字節緩沖區,最常用的緩沖區類型,用于對字節數據的讀寫操作。nxs28資訊網——每日最新資訊28at.com

import java.nio.ByteBuffer;public class ByteBufferExample {  public static void main(String[] args) {    // 創建一個新的字節緩沖區,初始容量為10個字節    ByteBuffer buffer = ByteBuffer.allocate(10);        // 向緩沖區中寫入4個字節    buffer.put((byte) 1);    buffer.put((byte) 2);    buffer.put((byte) 3);    buffer.put((byte) 4);        // 輸出緩沖區中的內容    buffer.flip(); // 將緩沖區切換成讀模式    System.out.println(buffer.get()); // 輸出1    System.out.println(buffer.get()); // 輸出2    System.out.println(buffer.get()); // 輸出3    System.out.println(buffer.get()); // 輸出4        // 將緩沖區清空并重新寫入數據    buffer.clear();    buffer.put((byte) 5);    buffer.put((byte) 6);    buffer.put((byte) 7);    buffer.put((byte) 8);        // 輸出緩沖區中的內容,方法同上    buffer.flip();    System.out.println(buffer.get()); // 輸出5    System.out.println(buffer.get()); // 輸出6    System.out.println(buffer.get()); // 輸出7    System.out.println(buffer.get()); // 輸出8  }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 在上面的示例中,我們使用ByteBuffer類的allocate()方法創建了一個新的字節緩沖區,然后向緩沖區中寫入4個字節的數據。
  • 接著,我們通過調用flip()方法將緩沖區切換成讀模式,并使用get()方法讀取緩沖區中的數據,并按順序輸出每個字節。
  • 最后,我們清空緩沖區并重新寫入數據,再次將緩沖區切換成讀模式,并使用get()方法讀取緩沖區中的數據。

2、CharBuffer示例代碼

字符緩沖區,用于對字符數據的讀寫操作。nxs28資訊網——每日最新資訊28at.com

import java.nio.CharBuffer;public class CharBufferExample {    public static void main(String[] args) {        // 創建一個新的字符緩沖區,初始容量為10個字符        CharBuffer buffer = CharBuffer.allocate(10);                // 向緩沖區中寫入4個字符        buffer.put('a');        buffer.put('b');        buffer.put('c');        buffer.put('d');                // 輸出緩沖區中的內容        buffer.flip(); // 將緩沖區切換成讀模式        System.out.println(buffer.get()); // 輸出a        System.out.println(buffer.get()); // 輸出b        System.out.println(buffer.get()); // 輸出c        System.out.println(buffer.get()); // 輸出d                // 將緩沖區清空并重新寫入數據        buffer.clear();        buffer.put('e');        buffer.put('f');        buffer.put('g');        buffer.put('h');                // 輸出緩沖區中的內容,方法同上        buffer.flip();        System.out.println(buffer.get()); // 輸出e        System.out.println(buffer.get()); // 輸出f        System.out.println(buffer.get()); // 輸出g        System.out.println(buffer.get()); // 輸出h    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 在上面的示例中,我們使用CharBuffer類的allocate()方法創建了一個新的字符緩沖區,然后向緩沖區中寫入4個字符的數據。
  • 接著,我們通過調用flip()方法將緩沖區切換成讀模式,并使用get()方法讀取緩沖區中的數據,并按順序輸出每個字符。
  • 最后,我們清空緩沖區并重新寫入數據,再次將緩沖區切換成讀模式,并使用get()方法讀取緩沖區中的數據。

3、ShortBuffer、IntBuffer、LongBuffer、FloatBuffer、DoubleBuffer 等示例代碼

import java.nio.*;public class BasicBufferExample {    public static void main(String[] args) {        // 創建各種基本數據類型的緩沖區,初始容量為10        ShortBuffer shortBuf = ShortBuffer.allocate(10);        IntBuffer intBuf = IntBuffer.allocate(10);        LongBuffer longBuf = LongBuffer.allocate(10);        FloatBuffer floatBuf = FloatBuffer.allocate(10);        DoubleBuffer doubleBuf = DoubleBuffer.allocate(10);                // 向緩沖區中寫入數據        shortBuf.put((short) 1);        intBuf.put(2);        longBuf.put(3L);        floatBuf.put(4.0f);        doubleBuf.put(5.0);                // 反轉緩沖區,切換到讀模式        shortBuf.flip();        intBuf.flip();        longBuf.flip();        floatBuf.flip();        doubleBuf.flip();                // 讀取緩沖區中的數據        System.out.println(shortBuf.get()); // 輸出1        System.out.println(intBuf.get()); // 輸出2        System.out.println(longBuf.get()); // 輸出3        System.out.println(floatBuf.get()); // 輸出4.0        System.out.println(doubleBuf.get()); // 輸出5.0    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 在上面的示例中,我們分別創建了ShortBuffer、IntBuffer、LongBuffer、FloatBuffer、DoubleBuffer等基本數據類型的緩沖區。
  • 接著,我們向這些緩沖區中寫入了對應數據類型的數據。
  • 然后我們通過調用flip()方法,將緩沖區切換成讀模式,并通過get()方法讀取緩沖區中的數據,并按順序輸出每一個數據類型的內容。

三、Selector

Selector 是 Java NIO 類庫中的一個重要組件,它用于監聽多個 Channel 的事件。在一個線程中,通過 Selector 可以監聽多個 Channel 的 IO 事件,并實現了基于事件響應的架構。Selector 可以讓單個線程處理多個 Channel,因此它可以提高多路復用的效率。nxs28資訊網——每日最新資訊28at.com

1、Selector讓單線程處理多個Channel的代碼示例

import java.io.IOException;import java.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.*;import java.util.Iterator;import java.util.Set;public class SelectorExample {    public static void main(String[] args) throws IOException {        // 創建一個ServerSocketChannel,監聽本地端口        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();        serverSocketChannel.socket().bind(new InetSocketAddress("localhost", 8080));        serverSocketChannel.configureBlocking(false);                // 創建一個Selector,并將serverSocketChannel注冊到Selector上        Selector selector = Selector.open();        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);        System.out.println("Server started on port 8080");        while (true) {            // 如果沒有任何事件發生,則阻塞等待            selector.select();            // 處理事件            Set<SelectionKey> selectedKeys = selector.selectedKeys();            Iterator<SelectionKey> keyIterator = selectedKeys.iterator();            while (keyIterator.hasNext()) {                SelectionKey key = keyIterator.next();                if (key.isAcceptable()) {                    // 處理新的連接請求                    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();                    SocketChannel clientChannel = serverChannel.accept();                    clientChannel.configureBlocking(false);                    System.out.println("Accepted connection from " + clientChannel.getRemoteAddress());                    clientChannel.register(selector, SelectionKey.OP_READ);                } else if (key.isReadable()) {                    // 處理讀事件                    SocketChannel clientChannel = (SocketChannel) key.channel();                    ByteBuffer buffer = ByteBuffer.allocate(1024);                    int bytesRead = clientChannel.read(buffer);                    String message = new String(buffer.array(), 0, bytesRead);                    System.out.println("Received message from " + clientChannel.getRemoteAddress() + ": " + message);                    // 回寫數據                    ByteBuffer outputBuffer = ByteBuffer.wrap(("Echo: " + message).getBytes());                    clientChannel.write(outputBuffer);                }                // 從待處理事件集合中移除當前事件                keyIterator.remove();            }        }    }}

2、示例代碼說明

  • 使用ServerSocketChannel監聽本地8080端口,并將ServerSocketChannel注冊到Selector上。
  • 在while循環中,我們通過調用select()方法等待事件發生,如果有事件發生,則從Selector中獲取待處理事件集合,然后遍歷事件集合,處理每個事件。
  • 如果當前事件是新的連接請求,則接受該連接,并將對應的SocketChannel注冊到Selector上,使用OP_READ模式表示可以讀取數據。
  • 如果當前事件是可讀的,則讀取SocketChannel中的數據并進行回寫,回寫時使用ByteBuffer包裝需要回寫的數據,并將其寫入到SocketChannel中。
  • 最后,我們從待處理事件集合中移除當前事件。

四、ZipInputStream 和 ZipOutputStream

ZipInputStream 和 ZipOutputStream 可以用于處理 ZIP 文件格式,ZipInputStream 可以從 ZIP 文件中讀取數據,ZipOutputStream 可以向 ZIP 文件中寫入數據。nxs28資訊網——每日最新資訊28at.com

1、ZipInputStream示例代碼

import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipExample {    public static void main(String[] args) throws IOException {        // 輸入文件路徑和輸出壓縮文件路徑        String inputFile = "/path/to/input/file";        String outputFile = "/path/to/output/file.zip";        // 創建ZipOutputStream,并設置壓縮級別        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(outputFile));        zipOutputStream.setLevel(9);        // 讀取需要壓縮的文件到文件輸入流        FileInputStream fileInputStream = new FileInputStream(inputFile);        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);        // 設置壓縮文件內部的名稱        ZipEntry zipEntry = new ZipEntry(inputFile);        zipOutputStream.putNextEntry(zipEntry);        // 寫入壓縮文件        byte[] buf = new byte[1024];        int len;        while ((len = bufferedInputStream.read(buf)) > 0) {            zipOutputStream.write(buf, 0, len);        }        bufferedInputStream.close();        zipOutputStream.closeEntry();        zipOutputStream.close();        System.out.println("File compressed successfully");    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 首先,我們創建ZipOutputStream并設置壓縮級別。
  • 接著,我們創建輸入文件的FileInputStream,并使用BufferedInputStream包裝它。
  • 我們接著設置壓縮文件內部的名稱,并使用zipOutputStream.putNextEntry()方法將其寫入ZipOutputStream中。
  • 最后,我們從緩沖區讀取文件數據并將其寫入ZipOutputStream中。最后關閉輸入流和ZipOutputStream。

2、ZipOutputStream示例代碼

import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;public class UnzipExample {    public static void main(String[] args) throws IOException {        // 輸入壓縮文件路徑和輸出文件路徑        String inputFile = "/path/to/input/file.zip";        String outputFile = "/path/to/output/file";        // 創建ZipInputStream        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(inputFile));        // 循環讀取壓縮文件中的條目        ZipEntry zipEntry = zipInputStream.getNextEntry();        while (zipEntry != null) {            // 如果是目錄,則創建空目錄            if (zipEntry.isDirectory()) {                new File(outputFile + File.separator + zipEntry.getName()).mkdirs();            } else { // 如果是文件,則輸出文件                FileOutputStream fileOutputStream = new FileOutputStream(outputFile + File.separator                        + zipEntry.getName());                byte[] buf = new byte[1024];                int len;                while ((len = zipInputStream.read(buf)) > 0) {                    fileOutputStream.write(buf, 0, len);                }                fileOutputStream.close();            }            zipInputStream.closeEntry();            zipEntry = zipInputStream.getNextEntry();        }        zipInputStream.close();        System.out.println("File uncompressed successfully");    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 使用ZipInputStream從指定輸入文件中解壓文件到指定的輸出文件夾中。
  • 我們創建ZipInputStream,然后循環讀取壓縮文件中的條目。如果當前條目是目錄,則創建空目錄,并使用mkdirs()方法創建目錄。如果當前條目是文件,則使用FileOutputStream將文件寫入到指定的輸出文件中。
  • 最后關閉當前ZipEntry,并通過getNextEntry()方法獲取ZipInputStream中的下一個條目。

五、GZIPInputStream 和 GZIPOutputStream

GZIPInputStream 和 GZIPOutputStream 可以用于進行 GZIP 壓縮,GZIPInputStream 可以從壓縮文件中讀取數據,GZIPOutputStream 可以將數據寫入壓縮文件中。nxs28資訊網——每日最新資訊28at.com

1、GZIPInputStream代碼示例

import java.io.*;import java.util.zip.GZIPOutputStream;public class GzipExample {    public static void main(String[] args) throws IOException {        // 輸入文件路徑和輸出壓縮文件路徑        String inputFile = "/path/to/input/file";        String outputFile = "/path/to/output/file.gz";        // 創建GZIPOutputStream,并設置壓縮級別        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(outputFile));        gzipOutputStream.setLevel(9);        // 讀取需要壓縮的文件到文件輸入流        FileInputStream fileInputStream = new FileInputStream(inputFile);        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);        // 寫入壓縮文件        byte[] buf = new byte[1024];        int len;        while ((len = bufferedInputStream.read(buf)) > 0) {            gzipOutputStream.write(buf, 0, len);        }        bufferedInputStream.close();        gzipOutputStream.close();        System.out.println("File compressed successfully");    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 使用GZIPOutputStream將指定的輸入文件壓縮成輸出文件。
  • 首先,創建GZIPOutputStream并設置壓縮級別。
  • 接著,創建輸入文件的FileInputStream,并使用BufferedInputStream包裝它。
  • 接著從緩沖區讀取文件數據并將其寫入GZIPOutputStream中。最后關閉輸入流和GZIPOutputStream。

2、GZIPOutputStream代碼示例

import java.io.*;import java.util.zip.GZIPInputStream;public class GunzipExample {    public static void main(String[] args) throws IOException {        // 輸入壓縮文件路徑和輸出文件路徑        String inputFile = "/path/to/input/file.gz";        String outputFile = "/path/to/output/file";        // 創建GZIPInputStream        GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inputFile));        // 輸出文件        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);        byte[] buf = new byte[1024];        int len;        while ((len = gzipInputStream.read(buf)) > 0) {            fileOutputStream.write(buf, 0, len);        }        gzipInputStream.close();        fileOutputStream.close();        System.out.println("File uncompressed successfully");    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 使用GZIPInputStream從指定輸入文件中解壓文件到指定的輸出文件中。
  • 首先,我們創建GZIPInputStream,然后從緩沖區讀取文件數據并將其寫入到指定的輸出文件中。
  • 最后,我們關閉輸入流和輸出流。

六、ByteArrayInputStream 和 ByteArrayOutputStream

ByteArrayInputStream 和 ByteArrayOutputStream 分別是 ByteArrayInputStream 和 ByteArrayOutputStream 類的子類,它們可以用于對字節數組進行讀寫操作。nxs28資訊網——每日最新資訊28at.com

1、ByteArrayInputStream 代碼示例

import java.io.ByteArrayInputStream;import java.io.ByteArrayInputStream;import java.io.IOException;public class ByteArrayInputStreamExample {    public static void main(String[] args) throws IOException {        // 用字符串初始化一個字節數組,作為輸入數據源        String input = "Hello, world!";        byte[] inputBytes = input.getBytes();        // 創建一個ByteArrayInputStream,使用輸入數據源        ByteArrayInputStream inputStream = new ByteArrayInputStream(inputBytes);        // 讀取并輸出輸入流中的數據        byte[] buf = new byte[1024];        int len;        while ((len = inputStream.read(buf)) != -1) {            System.out.println(new String(buf, 0, len));        }        // 關閉輸入流        inputStream.close();    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 使用“Hello, world!”字符串創建了一個字節數組作為輸入數據源,并使用ByteArrayInputStream將其包裝成輸入流。
  • 使用一個循環從輸入流中讀取數據,并使用new String()方法將其轉換成字符串并輸出到控制臺。
  • 最后,關閉輸入流。

2、ByteArrayOutputStream代碼示例

import java.io.ByteArrayOutputStream;import java.io.IOException;public class ByteArrayOutputStreamExample {    public static void main(String[] args) {        String input = "Hello World!";        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();        byte[] output;        try {            outputStream.write(input.getBytes());            output = outputStream.toByteArray();            System.out.println(new String(output));        } catch (IOException e) {            System.out.println("Error: " + e.getMessage());        } finally {            try {                outputStream.close();            } catch (IOException e) {                System.out.println("Error: " + e.getMessage());            }        }    }}

示例代碼說明:nxs28資訊網——每日最新資訊28at.com

  • 在這個例子中,創建了一個ByteArrayOutputStream對象 outputStream,并向其寫入一個字符串"Hello World!"。然后,我們使用toByteArray()方法將結果轉換為一個字節數組,并打印出來。
  • 注意:在使用ByteArrayOutputStream時,要確保在不再需要它時關閉它以確保所有的字節都被刷新到輸出流中。

七、總結

本文為您講解了 Java I/O、NIO 以及其他一些流的基本概念、用法和區別。Java I/O 和 NIO 可以完成很多復雜的輸入輸出操作,包括文件操作、網絡編程、序列化等。其他流技術可以實現壓縮、讀寫字節數組等功能。在進行開發時,根據具體需求選擇不同的流技術可以提高程序效率和開發效率。nxs28資訊網——每日最新資訊28at.com

本文轉載自微信公眾號「哪吒編程」,可以通過以下二維碼關注。轉載本文請聯系哪吒編程公眾號。nxs28資訊網——每日最新資訊28at.com

nxs28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-150-0.html一文搞定Java NIO,以及各種奇葩流

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 微軟邀請 Microsoft 365 商業用戶,測試視頻編輯器 Clipchamp

下一篇: JVM優化:實戰OutOfMemoryError異常

標簽:
  • 熱門焦點
  • 鴻蒙OS 4.0公測機型公布:甚至連nova6都支持

    華為全新的HarmonyOS 4.0操作系統將于今天下午正式登場,官方在發布會之前也已經正式給出了可升級的機型產品,這意味著這些機型會率先支持升級享用。這次的HarmonyOS 4.0支持
  • 2023年Q2用戶偏好榜:12+256G版本成新主流

    3月份的性能榜、性價比榜和好評榜之后,就要輪到2023年的第二季度偏好榜了,上半年的新機潮已經過去,最明顯的肯定就是大內存和存儲的機型了,另外部分中端機也取消了屏幕塑料支架
  • 學習JavaScript的10個理由...

    作者 | Simplilearn編譯 | 王瑞平當你決心學習一門語言的時候,很難選擇到底應該學習哪一門,常用的語言有Python、Java、JavaScript、C/CPP、PHP、Swift、C#、Ruby、Objective-
  • 品牌洞察丨服務本地,美團直播成效幾何?

    來源:17PR7月11日,美團App首頁推薦位出現&ldquo;美團直播&rdquo;的固定入口。在直播聚合頁面,外賣&ldquo;神槍手&rdquo;直播間、美團旅行直播間、美團買菜直播間等均已上線,同時
  • ESG的面子與里子

    來源 | 光子星球撰文 | 吳坤諺編輯 | 吳先之三伏大幕拉起,各地高溫預警不絕,但處于厄爾尼諾大&ldquo;烤&rdquo;之下的除了眾生,還有各大企業發布的ESG報告。ESG是&ldquo;環境保
  • 蘋果、三星、惠普等暫停向印度出口筆記本和平板電腦

    集微網消息,據彭博社報道,在8月3日印度突然禁止在沒有許可證的情況下向印度進口電腦/平板及顯示器等產品后,蘋果、三星電子和惠普等大公司暫停向印度
  • 三星Galaxy Z Fold/Flip 5國行售價曝光 :最低7499元/12999元起

    據官方此前宣布,三星將于7月26日也就是明天在韓國首爾舉辦Unpacked活動,屆時將帶來帶來包括Galaxy Buds 3、Galaxy Watch 6、Galaxy Tab S9、Galaxy
  • 三星顯示已開始為AR設備研發硅基LED微顯示屏

    7月18日消息,據外媒報道,隨著蘋果首款頭顯產品Vision Pro在6月份正式推出,AR/VR/MR等頭顯產品也就將成為各大公司下一個重要的競爭領域,對顯示屏這一關
  • Windows 11發布,微軟一改往常對老機型開放的態度

    距離 Windows 11 發布已經過去一周,在過去一周里,很多數碼愛好者圍繞其對 Android 應用的支持、對老機型的升級問題展開了激烈討論。與以往不同的是,在這次大
Top