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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

2-字节流

發(fā)布時間:2024/4/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2-字节流 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package com.io;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;import org.junit.Test; /*** * @author Administrator*1、流的分類*(1)按照數(shù)據(jù)流向的不同,分為輸入流和輸出流*(2)按照處理數(shù)據(jù)單位的不同,分為字節(jié)流和字符流*(3)按照角色的不同,分為節(jié)點流(直接作用于文件,所有帶File的InputStream OutputStream)、處理流(作用于節(jié)點流上面,提高效率)*2、IO體系*抽象基類 節(jié)點流(即文件流) 緩總流(處理流的一種)*InputStream FileInputStream BufferedInputStream*OutputStream FileOutputStream BufferedOutputStream*Reader FileReader BufferedReader*Writer FileWriter BufferedWriter*3、所有的處理流都作用于上面四種節(jié)點流,才能進行對文件操作*/ public class FileInputOurPutStreamTest {/*** 每次讀一個字節(jié)*/@Testpublic void fileInputStreamTest1(){FileInputStream fis = null;//1、先創(chuàng)建file對象try { //有異常在這里捕獲,因為fis使用完后一定要關閉File file1 = new File("hello1.txt");//2、創(chuàng)建FileInputStream對象fis = new FileInputStream(file1);/**int b = fis.read();//read方法,一次讀取一個字節(jié),讀到文件最后返回-1while(b != -1){ // System.out.println(b);//因為讀取的是字節(jié),這里用int接收,所以打印出來是unicode碼,要顯示字母,加char強制轉化System.out.println((char)b);b = fis.read();}**///上面代碼可以簡寫成int b ;while( (b = fis.read()) != -1 ){System.out.print((char)b);}} catch ( IOException e) {e.printStackTrace();}finally{try {fis.close();} catch (IOException e) {e.printStackTrace();}}}/*** 每次讀一個數(shù)組長度的字節(jié)*/@Testpublic void fileInputStreamTest2(){File file = new File("hello1.txt");FileInputStream fis = null;try{fis = new FileInputStream(file);byte[] b = new byte[5];int len;//len是每次讀出放到數(shù)組中的字節(jié)數(shù),當?shù)轿募┪驳臅r候返回-1,前幾個len都返回的是數(shù)組的長度,最后一個返回的len<=數(shù)組長度StringBuffer strTmp = new StringBuffer("");while( (len = fis.read(b)) != -1 ){System.out.println(len);String str = new String(b, 0, len);//這里要注意new String的三個參數(shù),第一個是數(shù)組,第二個是從數(shù)組的第幾個下標開始讀,最后一個,是最后一個數(shù)組的長度 System.out.println(str);strTmp.append(str);}System.out.println("strTmp-===" + strTmp);}catch(Exception e){e.printStackTrace();}finally{try {fis.close();} catch (IOException e) {e.printStackTrace();}}}@Testpublic void fileOutPutStreamTest(){File file = new File("hello_out.txt");FileOutputStream fos = null;try{fos = new FileOutputStream(file);byte[] strTmp = new String("I love china").getBytes();fos.write(strTmp);}catch(Exception e){e.printStackTrace();}finally{try {fos.close();} catch (IOException e) {e.printStackTrace();}}}/*** 從一個文件讀取內容,寫入到另外一個文件* 即文件的復制*/@Testpublic void fileInputOutputStreamTest(){FileInputStream fis = null;FileOutputStream fos = null;File fileIn = new File("hello1.txt");File fileout = new File("hello2.txt");try{fis = new FileInputStream(fileIn);fos = new FileOutputStream(fileout);int len;byte [] b = new byte[20];//根據(jù)文件大小來設定數(shù)組的大小while((len = fis.read(b)) != -1){fos.write(b, 0, len);//0是從字節(jié)數(shù)組b的的第0位開始 }}catch(Exception e){e.printStackTrace();}finally{try {fis.close();} catch (IOException e) {e.printStackTrace();}try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}

?

轉載于:https://www.cnblogs.com/fubaizhaizhuren/p/5026116.html

總結

以上是生活随笔為你收集整理的2-字节流的全部內容,希望文章能夠幫你解決所遇到的問題。

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