java 中 的 字节流!
生活随笔
收集整理的這篇文章主要介紹了
java 中 的 字节流!
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package cn.zhouzhou;import java.io.FileInputStream;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import javax.management.RuntimeErrorException;/** 一、流?* 流是一組有順序的,有起點(diǎn)和終點(diǎn)的字節(jié)集合,是對(duì)數(shù)據(jù)傳輸?shù)目偡Q(chēng)或抽象。* 即數(shù)據(jù)在兩設(shè)備間的傳輸稱(chēng)為流,流的本質(zhì)是數(shù)據(jù)傳輸,根據(jù)數(shù)據(jù)傳輸特性將流抽象為各種類(lèi),* 方便更直觀的進(jìn)行數(shù)據(jù)操作。*二、 字節(jié)流?* 先讀后寫(xiě)!* 1.輸出流-------程序到文件------OutputStream類(lèi)!寫(xiě)* 1.OutputStream 2.加上代表?yè)Q行!【\r\n】* * 2.輸入流-------文件到程序-------InputStream類(lèi)!讀* a.方法 read() 返回值-1 利用while循環(huán) 讀取文件!int len=0; 返回值!* * b.讀取字節(jié)數(shù)組 * byte b[]=new byte[1024]; * int len=0; while((len=a1.read(b))!=-1);* System.out.println(new String(b,0,len));* 三、文件復(fù)制?* 方法:* 1.用InputStream讀取,OutputStream寫(xiě)!* 2.創(chuàng)建數(shù)組緩沖!* byte b[]=new byte[1024*10]; * int len=0; * while((len=a1.read(b))!=-1);* {a2.write(b,0,len)}* * */
public class OutputStreamDemo {public static void main(String[] args) throws IOException {run01();//1.輸出流 寫(xiě)!OutputStream write();run02();//2.輸入流 讀!InputStream read(); run03();//3.讀取字節(jié)數(shù)組 byte [] b=new byte[1024];copy();// 4.簡(jiǎn)單的文件復(fù)制 異常拋出!copy01();//5.利用 try catch解決異常
}//"E:\\老師的代碼\\day20\\視屏\\001.mp4" "d:\\game\\001.mp4"private static void copy01() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("E:\\老師的代碼\\day20\\視屏\\001.mp4");fos = new FileOutputStream("d:\\game\\001.mp4");int len = 0;byte []b=new byte[1024*10000]; //復(fù)制速度很快!while ((len = fis.read(b)) != -1) {fos.write(b,0,len);}} catch (IOException ex) {System.out.println(ex);throw new RuntimeException("文件復(fù)制失敗");} finally {try {if (fos != null) {fos.close();}} catch (IOException ex) {throw new RuntimeException("釋放資源失敗");} finally {try {if (fis != null) {fis.close();}} catch (IOException ex) {throw new RuntimeException("釋放資源失敗");}}}}private static void copy() throws IOException {//想要復(fù)制 先讀后寫(xiě)!FileInputStream a1=null;FileOutputStream a2=null;a1=new FileInputStream("E:\\老師的代碼\\day25\\視屏\\1.mp4");a2=new FileOutputStream("d:\\game\\1.mp4");//定義一個(gè)數(shù)組,緩沖![1024*10]就夠了byte []b=new byte[1024*10000]; //1024*10000 復(fù)制速度很快!我的計(jì)算機(jī)有些卡頓!int len=0;while ((len=a1.read(b))!=-1) {a2.write(b,0,len);}a1.close();a2.close();}private static void run03() throws IOException {FileInputStream a1=new FileInputStream("d:\\game\\aaa.txt");//創(chuàng)建字節(jié)數(shù)組, 一般都是1024 讀取速度快!byte [] b=new byte[1024];int len=0;while ((len=a1.read(b))!=-1) {System.out.println(new String(b,0,len)); //new String(b,0,len);數(shù)組byte中 字節(jié)從0開(kāi)始,到len 最后一個(gè)!
}a1.close();}private static void run02()throws IOException {FileInputStream a1=new FileInputStream("d:\\game\\hello.txt"); //讀取文件aaa.txt.文本內(nèi)容是中文 有時(shí)會(huì)有亂碼!--->以后解決。int len=0; //讀取一個(gè)字節(jié)的方法 read() 返回值 int int len=0;實(shí)在接受read方法的返回值!while ((len =a1.read())!=-1) { //a1.read() 賦值給len 當(dāng)len等于-1時(shí),循環(huán)結(jié)束! System.out.print((char)len); //使用循環(huán),讀取文件,循環(huán)結(jié)束的條件 read()方法返回值為-1
}a1.close(); //流 有始有終的!
}public static void run01() throws IOException{//創(chuàng)建文件! 在 內(nèi)存中寫(xiě)入到硬盤(pán)! OutputStream();FileOutputStream a1=new FileOutputStream("d:\\game\\aaa.txt");//在文件aaa.TXT中寫(xiě)入東西。 添加97 在字節(jié)中是a。 這種寫(xiě)法一次只能添加一個(gè),慢!a1.write(97);//創(chuàng)建字節(jié)數(shù)組 ,寫(xiě)的就快! 一次傳遞很多個(gè)字節(jié)!byte []bytes={98,99,97};//(bytes,0,1); 獲取bytes數(shù)組中,角標(biāo)從0 到2的元素!注意。取不到角標(biāo)為2的元素! 有頭無(wú)尾!a1.write(bytes, 0, 3);System.out.println("b是"+bytes[0]+" c是"+bytes[1]+" a是"+bytes[2]);//寫(xiě)入 字符串。不能直接寫(xiě)入,需要先轉(zhuǎn)換成bytes!a1.write("你好\r\n".getBytes()); // \r\n 換行!
a1.write("世界!".getBytes());a1.close(); //輸出流打開(kāi)后,需要關(guān)閉!
}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/ZXF6/p/10596271.html
總結(jié)
以上是生活随笔為你收集整理的java 中 的 字节流!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 短信验证
- 下一篇: webpack [记录]