(JAVA)IO缓冲区
生活随笔
收集整理的這篇文章主要介紹了
(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缓冲区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是压力测试,如何做压力测试?
- 下一篇: 超声换能器的原理及设计_超声波发生器、变