<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## Java專題十一(1):IO [TOC] > IO是基于字節流和字符流操作的 ![](https://img.kancloud.cn/f7/62/f7622fae823b204d6c5a23bb595ac1bc_674x749.png) **IO基類**: | | Reader |Writer |InputStream |OutputStream | | --- | --- | --- | --- | --- | | 字符流 | ? | ? | | | | 字節流 | | | ? | ? | | 輸入流 | ? | | ? | | | 輸出流 | | ? | | ? | **各種資源IO類**: | | Reader |Writer |InputStream |OutputStream | | --- | --- | --- | --- | --- | | File | FileReader | FileWriter| FileInputStream | FileOutputStream | | Buffer | BufferReader | BufferWriter | BufferedInputStream | BufferedOutputStream | | Pipe | PipedReader | PipedWriter| PipedInputStream | PipedOutputStream | | String | StringReader | StringWriter| | | | ByteArray | ByteArrayReader | ByteArrayWriter| ByteArrayInputStream | ByteArrayOutputStream | | CharArray | CharArrayReader | CharArrayWriter| | | | Object | | |ObjectInputStream | ObjectOutputStream | | Filter | FilterReader | FilterWriter| FilterInputStream | FilterOutputStream | | Data | | | DataInputStream | DataOutputStream | ### 1.字節流轉換成字符(InputStreamReader) ``` public static String byte2Char(InputStream is, String charsetName) throws IOException { if (charsetName == null || !Charset.isSupported(charsetName)){ charsetName = DEFAULT_CHARSET; } InputStreamReader isr = new InputStreamReader(is, charsetName); char[] cbuf = new char[DEFAULT_BYTE_SIZE]; StringBuilder sbf = new StringBuilder(); int readCount; while((readCount = isr.read(cbuf)) > 0){ sbf.append(cbuf, 0, readCount); } return sbf.toString(); } ``` ### 2.從文件中讀取字符(FileReader) ``` public static String readCharFromFile(String path) throws IOException { File fi = new File(path); if (fi.exists() && fi.isFile()){ FileReader fr = new FileReader(fi); char[] cbuf = new char[DEFAULT_CHAR_SIZE]; int readCount; StringBuilder sbf = new StringBuilder(); while((readCount = fr.read(cbuf)) > 0){ sbf.append(cbuf, 0, readCount); } return sbf.toString(); } throw new FileNotFoundException(); } ``` ### 3.字節流輸出到文件(FileOutputStream) ``` public static File writeByteToFile(InputStream is, String path, boolean append) throws IOException { File fi = new File(path); // create file first if not exists if (!fi.exists()) { fi.createNewFile(); append = false; } FileOutputStream fos = new FileOutputStream(fi, append); byte[] buf = new byte[DEFAULT_BYTE_SIZE]; int readCount; while((readCount = is.read(buf)) > 0){ fos.write(buf, 0, readCount); } fos.close(); is.close(); return fi; } ``` ### 4.字符輸出到文件(FileWriter) ``` public static File writeCharToFile(String content, String path, boolean append) throws IOException { File fi = new File(path); // create file first if not exists if (!fi.exists()) { fi.createNewFile(); append = false; } FileWriter fw = new FileWriter(fi, append); fw.write(content, 0, content.length()); fw.close(); return fi; } ``` 字節流轉換成字符流,文件與字節,文件與字符轉換操作完整代碼見: [IOTools](https://github.com/15045120/git-docs/blob/master/tools/IOTools.java)
                  <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>

                              哎呀哎呀视频在线观看