<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                <!-- Converting Data --> ## 數據轉換 為了將 **GetChannel.java** 文件中的信息打印出來。在 Java 中,我們每次提取一個字節的數據并將其轉換為字符。看起來很簡單 —— 如果你有看過 `ava.nio.`**CharBuffer** 類,你會發現一個 `toString()` 方法。該方法的作用是“返回一個包含此緩沖區字符的字符串”。 既然 **ByteBuffer** 可以通過 **CharBuffer** 類的 `asCharBuffer()` 方法查看,那我們就來嘗試一樣。從下面輸出語句的第一行可以看出,這并不正確: ```java // newio/BufferToText.java // (c)2017 MindView LLC: see Copyright.txt // 我們無法保證該代碼是否適用于其他用途。 // 訪問 http://OnJava8.com 了解更多本書信息。 // text 和 ByteBuffers 互轉 import java.nio.*; import java.nio.channels.*; import java.nio.charset.*; import java.io.*; public class BufferToText { private static final int BSIZE = 1024; public static void main(String[] args) { try( FileChannel fc = new FileOutputStream( "data2.txt").getChannel() ) { fc.write(ByteBuffer.wrap("Some text".getBytes())); } catch(IOException e) { throw new RuntimeException(e); } ByteBuffer buff = ByteBuffer.allocate(BSIZE); try( FileChannel fc = new FileInputStream( "data2.txt").getChannel() ) { fc.read(buff); } catch(IOException e) { throw new RuntimeException(e); } buff.flip(); // 無法運行 System.out.println(buff.asCharBuffer()); // 使用默認系統默認編碼解碼 buff.rewind(); String encoding = System.getProperty("file.encoding"); System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff)); // 編碼和打印 try( FileChannel fc = new FileOutputStream( "data2.txt").getChannel() ) { fc.write(ByteBuffer.wrap( "Some text".getBytes("UTF-16BE"))); } catch(IOException e) { throw new RuntimeException(e); } // 嘗試再次讀取: buff.clear(); try( FileChannel fc = new FileInputStream( "data2.txt").getChannel() ) { fc.read(buff); } catch(IOException e) { throw new RuntimeException(e); } buff.flip(); System.out.println(buff.asCharBuffer()); // 通過 CharBuffer 寫入: buff = ByteBuffer.allocate(24); buff.asCharBuffer().put("Some text"); try( FileChannel fc = new FileOutputStream( "data2.txt").getChannel() ) { fc.write(buff); } catch(IOException e) { throw new RuntimeException(e); } // 讀取和顯示: buff.clear(); try( FileChannel fc = new FileInputStream( "data2.txt").getChannel() ) { fc.read(buff); } catch(IOException e) { throw new RuntimeException(e); } buff.flip(); System.out.println(buff.asCharBuffer()); } } ``` 輸出結果: ``` ???? Decoded using windows-1252: Some text Some text Some textNULNULNUL ``` 緩沖區包含普通字節,為了將這些字節轉換為字符,我們必須在輸入時對它們進行編碼(這樣它們輸出時就有意義了),或者在輸出時對它們進行解碼。我們可以使用 `java.nio.charset.`**Charset** 字符集工具類來完成。代碼示例: ```java // newio/AvailableCharSets.java // (c)2017 MindView LLC: see Copyright.txt // 我們無法保證該代碼是否適用于其他用途。 // 訪問 http://OnJava8.com 了解更多本書信息。 // 展示 Charsets 和 aliases import java.nio.charset.*; import java.util.*; public class AvailableCharSets { public static void main(String[] args) { SortedMap<String,Charset> charSets = Charset.availableCharsets(); for(String csName : charSets.keySet()) { System.out.print(csName); Iterator aliases = charSets.get(csName) .aliases().iterator(); if(aliases.hasNext()) System.out.print(": "); while(aliases.hasNext()) { System.out.print(aliases.next()); if(aliases.hasNext()) System.out.print(", "); } System.out.println(); } } } ``` 輸出結果: ``` Big5: csBig5 Big5-HKSCS: big5-hkscs, big5hk, Big5_HKSCS, big5hkscs CESU-8: CESU8, csCESU-8 EUC-JP: csEUCPkdFmtjapanese, x-euc-jp, eucjis, Extended_UNIX_Code_Packed_Format_for_Japanese, euc_jp, eucjp, x-eucjp EUC-KR: ksc5601-1987, csEUCKR, ksc5601_1987, ksc5601, 5601, euc_kr, ksc_5601, ks_c_5601-1987, euckr GB18030: gb18030-2000 GB2312: gb2312, euc-cn, x-EUC-CN, euccn, EUC_CN, gb2312-80, gb2312-1980 ... ``` 回到 **BufferToText.java** 中,如果你 `rewind()` 緩沖區(回到數據的開頭),使用該平臺的默認字符集 `decode()` 數據,那么生成的 **CharBuffer** 數據將在控制臺上正常顯示。可以通過 `System.getProperty(“file.encoding”)` 方法來查看平臺默認字符集名稱。傳遞該名稱參數到 `Charset.forName()` 方法可以生成對應的 `Charset` 對象用于解碼字符串。 另一種方法是使用字符集 `encode()` 方法,該字符集在讀取文件時生成可打印的內容,如你在 **BufferToText.java** 的第三部分中所看到的。上例中,**UTF-16BE** 被用于將文本寫入文件,當文本被讀取時,你所要做的就是將其轉換為 **CharBuffer**,并生成預期的文本。 最后,如果將 **CharBuffer** 寫入 **ByteBuffer**,你會看到發生了什么(更多詳情,稍后了解)。**注意**,為 **ByteBuffer** 分配了24個字節,按照每個字符占用 2 個自字節, 12 個字符的空間已經足夠了。由于“some text”只有 9 個字符,受其 `toString()` 方法影響,剩下的 0 字節部分也出現在了 **CharBuffer** 的展示中,如輸出所示。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看