日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(JAVA)IO缓冲区

發布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (JAVA)IO缓冲区 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package IODemo;import java.io.*; import java.nio.charset.StandardCharsets;/*** @author Alina* @date 2021年10月18日 9:57 下午* 一、 1.IO流關于緩沖區,* 2.輸出流緩沖區:BufferedOutputStream(OutputStream out)* 3.輸入流緩沖區:BufferedInputStream(InputStream in)* 4.提高流對象傳遞效率***/ public class FileIOStream {public static void main(String[] args) {long startime = System.currentTimeMillis();try {method_copy3();} catch (Exception e) {e.printStackTrace();}long endtime = System.currentTimeMillis();System.out.println(endtime - startime);}public static void method_bufferedout() throws Exception {//先創建OutputStream()對象,緩沖區方法依賴于流對象FileOutputStream fos = new FileOutputStream("/Users/yuzhang/Desktop/test.txt");BufferedOutputStream bos = new BufferedOutputStream(fos);bos.write("你好".getBytes(StandardCharsets.UTF_8));bos.close();}public static void method_bufferedin() throws Exception {FileInputStream fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");BufferedInputStream bis = new BufferedInputStream(fis); // int s = 0; // while ((s = bis.read()) != -1){ // System.out.print((char)s); // } // bis.close();byte[] bytes = new byte[1024];int x = 0;while ((x = bis.read(bytes)) != -1) {System.out.print(new String(bytes, 0, x));}bis.close();}public static void method_copy1() {//先創建兩個流向,先賦值為空,以避免作用域不同導致無法使用FileInputStream fis = null;FileOutputStream fop = null;try {fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");fop = new FileOutputStream("src/IOdemo/test1.txt");//創建字節int bytes = 0;while ((bytes = fis.read()) != -1) {fop.write(bytes);}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("復制失敗");} finally {try {if (fop !=null) {fop.close();}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("復制失敗");} finally {try{if (fis != null) {fis.close();}}catch (Exception e ){e.printStackTrace();throw new RuntimeException("關閉數據源失敗");}}}}public static void method_copy2(){//讀寫字節數組FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");fos = new FileOutputStream("src/IOdemo/test1.txt");byte[] bytes = new byte[1024];int len = 0;while ((len = fis.read(bytes))!= -1){fos.write( bytes,0,len);}}catch (Exception e){e.printStackTrace();throw new RuntimeException("復制失敗");}finally {try{if(fos !=null ){fos.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("關閉資源失敗");}finally {try {if (fis != null){fis.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("關閉資源失敗");}}}}public static void method_copy3(){BufferedInputStream bis = null;BufferedOutputStream bos = null;try{bis = new BufferedInputStream(new FileInputStream("/Users/yuzhang/Desktop/test.txt"));bos = new BufferedOutputStream(new FileOutputStream("src/IOdemo/test1.txt"));byte[] bytes = new byte[1024];int len = 0;while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}}catch (Exception e ){e.printStackTrace();throw new RuntimeException(" 復制失敗");}finally {try{if (bos != null){bos.close();}}catch (Exception e ){e.printStackTrace();throw new RuntimeException("資源關閉異常");}finally {try{if (bis != null){bis.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("資源關閉異常");}}}}}

總結

以上是生活随笔為你收集整理的(JAVA)IO缓冲区的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。