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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 字節流 ## 字節輸出流OutputStream OutputStream此抽象類,是表示輸出字節流的所有類的超類。操作的數據都是字節,定義了輸出字節流的基本共性功能方法。 輸出流中定義都是寫write方法 ![](https://box.kancloud.cn/58d8af3cc572de34638ed5918b4854d5_912x322.png) **FileOutputStream類** OutputStream有很多子類,其中子類FileOutputStream可用來寫入數據到文件。 FileOutputStream類,即文件輸出流,是用于將數據寫入 File的輸出流。 ![](https://box.kancloud.cn/903e6ea5cdd785dd5d6c4c24b6555848_932x313.png) 在FileOutputStream的構造函數中,可以接受一個boolean類型的值,如果值true,就會在文件末位繼續添加 ![](https://box.kancloud.cn/56c9b89c2697edd133f4dfbb677c05a2_916x149.png) ~~~ public static void main(String[] args) throws Exception { File file = new File("c:\\file.txt"); //第二個參數表示是否追加 FileOutputStream fos = new FileOutputStream(file, true); String str = "\r\n"+"java"; fos.write(str.getBytes()); fos.close(); } ~~~ ## IO異常處理 ~~~ public static void main(String[] args) { File file = new File("c:\\file.txt"); //定義FileOutputStream的引用 FileOutputStream fos = null; try { //創建FileOutputStream對象 fos = new FileOutputStream(file); //寫出數據 fos.write("abcde".getBytes()); } catch (IOException e) { System.out.println(e.toString() + "----"); } finally { //一定要判斷fos是否為null,只有不為null時,才可以關閉資源 if (fos != null) { try { fos.close(); } catch (IOException e) { throw new RuntimeException(""); } } } } ~~~ ## 字節輸入流InputStream InputStream此抽象類,是表示字節輸入流的所有類的超類。,定義了字節輸入流的基本共性功能方法 ![](https://box.kancloud.cn/c51871687024851e9bff338acb46b082_903x134.png) * int read():讀取一個字節并返回,沒有字節返回-1. * int read(byte[]): 讀取一定量的字節數,并存儲到字節數組中,返回讀取到的字節數 **FileInputStream類** InputStream有很多子類,其中子類FileInputStream可用來讀取文件內容。 FileInputStream 從文件系統中的某個文件中獲得輸入字節。 ![](https://box.kancloud.cn/99a24df9de188006f740691831744739_913x247.png) **FileInputStream類讀取數據read方法** 在讀取文件中的數據時,調用read方法,實現從文件中讀取數據 ![](https://box.kancloud.cn/c81df3273ef05a6c20a133e3eaec1e13_795x84.png) * 從文件中讀取數據,代碼演示 ~~~ public static void main(String[] args) throws IOException { String pathName = "/Users/jdxia/Desktop/study/javaStudy/src"; FileInputStream fileInputStream = new FileInputStream(pathName+"/index.txt"); int ch = 0; while ((ch = fileInputStream.read()) != -1) { System.out.print((char) ch); } //關閉資源 fileInputStream.close(); } ~~~ **讀取數據read(byte[])方法** 在讀取文件中的數據時,調用read方法,每次只能讀取一個,太麻煩了,于是我們可以定義數組作為臨時的存儲容器,這時可以調用重載的read方法,一次可以讀取多個字符。 ![](https://box.kancloud.cn/f64dcb3e178c0c356d4fbe783f8b974c_878x69.png) ~~~ public static void main(String[] args) throws IOException { /* * 演示第二個讀取方法, read(byte[]); */ File file = new File("c:\\file.txt"); // 創建一個字節輸入流對象,必須明確數據源,其實就是創建字節讀取流和數據源相關聯。 FileInputStream fis = new FileInputStream(file); //創建一個字節數組。 byte[] buf = new byte[1024];//長度可以定義成1024的整數倍。 int len = 0; while((len=fis.read(buf))!=-1){ System.out.println(new String(buf,0,len)); } fis.close(); } ~~~ # 復制文件 ~~~ public static void main(String[] args) throws IOException { //1,明確源和目的。 File srcFile = new File("c:\\YesDir\test.JPG"); File destFile = new File("copyTest.JPG"); //2,明確字節流 輸入流和源相關聯,輸出流和目的關聯。 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); //3, 使用輸入流的讀取方法讀取字節,并將字節寫入到目的中。 int ch = 0; while((ch=fis.read())!=-1){ fos.write(ch); } //4,關閉資源。 fos.close(); fis.close(); } ~~~ 上述代碼輸入流和輸出流之間是通過ch這個變量進行數據交換的。 上述復制文件有個問題,每次都從源文件讀取一個,然后在寫到指定文件,接著再讀取一個字符,然后再寫一個,一直這樣下去。效率極低。 **緩沖數組方式復制文件** 上述代碼復制文件效率太低了,并且頻繁的從文件讀數據,和寫數據,能不能一次多把文件中多個數據都讀進內容中,然后在一次寫出去,這樣的速度一定會比前面代碼速度快。 ~~~ public static void main(String[] args) throws IOException { File srcFile = new File("c:\\YesDir\test.JPG"); File destFile = new File("copyTest.JPG"); // 明確字節流 輸入流和源相關聯,輸出流和目的關聯。 FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); //定義一個緩沖區。 byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len);// 將數組中的指定長度的數據寫入到輸出流中。 } // 關閉資源。 fos.close(); fis.close(); } ~~~
                  <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>

                              哎呀哎呀视频在线观看