java13 InputStream,Reader
生活随笔
收集整理的這篇文章主要介紹了
java13 InputStream,Reader
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
流的方向:
1.輸入流:數(shù)據(jù)源到程序(InputStream,Reader讀進(jìn)來(lái))。
2.輸出流:程序到目的地(OutPutStream,Writer寫(xiě)出來(lái))。
處理數(shù)據(jù)單元:
字節(jié)流:按照字節(jié)讀取數(shù)據(jù)(InputStream,OutPutStream)。
字符流:按照字符讀取數(shù)據(jù)(Reader,Writer)
功能不同:
節(jié)點(diǎn)流:可以直接從數(shù)據(jù)源或目的地讀寫(xiě)數(shù)據(jù)。
處理流:不直接連接到數(shù)據(jù)源或者目的地,是處理流的流,通過(guò)對(duì)其他流的處理提高程序的性能。處理流:增強(qiáng)功能,提供性能,處理流在節(jié)點(diǎn)流之上。一、緩沖流
1)針對(duì)字節(jié)有字節(jié)緩沖流
BufferedInputStream readLine()
BufferedOutPutStream
2)針對(duì)字符有字符緩沖流
BufferedReader newLine()
BufferedWriter/*** 字節(jié)流文件拷貝+緩沖流 ,以后使用建議加上緩沖流提高性能。* 節(jié)點(diǎn)流上面包一層緩沖流。*/
public class BufferedByteDemo {public static void main(String[] args) {}/*** 文件的拷貝* @param 源文件路徑* @param 目錄文件路徑* @throws FileNotFoundException,IOException* @return */public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {//1、建立聯(lián)系 源(存在且為文件) +目的地(文件可以不存在) File src =new File(srcPath);File dest =new File(destPath);if(! src.isFile()){ //不是文件或者為nullSystem.out.println("只能拷貝文件");throw new IOException("只能拷貝文件");}//2、選擇流,利用緩沖流提高性能,InputStream is =new BufferedInputStream(new FileInputStream(src));OutputStream os =new BufferedOutputStream( new FileOutputStream(dest));//3、文件拷貝 循環(huán)+讀取+寫(xiě)出byte[] flush =new byte[1024];int len =0;//讀取while(-1!=(len=is.read(flush))){//寫(xiě)出os.write(flush, 0, len);}os.flush(); //強(qiáng)制刷出 //關(guān)閉流
os.close();is.close();}
}/*** 字符緩沖流 +新增方法(不能發(fā)生多態(tài)),字符流外面包一層緩沖流。*/
public class BufferedCharDemo {public static void main(String[] args) {//創(chuàng)建源 僅限于 字符的純文本File src =new File("E:/xp/test/1.java");File dest =new File("e:/xp/test/2.txt");//選擇流BufferedReader reader =null; BufferedWriter wr =null;try {reader =new BufferedReader(new FileReader(src));wr =new BufferedWriter(new FileWriter(dest));//讀取操作/*char[] flush =new char[1024];int len =0;while(-1!=(len=reader.read(flush))){wr.write(flush, 0, len);}*///新增方法的操作String line =null;while(null!=(line=reader.readLine())){//line每次為一行內(nèi)容
wr.write(line);//wr.append("\r\n");wr.newLine(); //換行符號(hào)} wr.flush();//強(qiáng)制刷出,流關(guān)閉的時(shí)候也可以刷出,這里是養(yǎng)成良好的編程習(xí)慣。} catch (FileNotFoundException e) {e.printStackTrace();System.out.println("源文件不存在");} catch (IOException e) {e.printStackTrace();System.out.println("文件讀取失敗");}finally{try {if (null != wr) {wr.close();}} catch (Exception e2) {}try {if (null != reader) {reader.close();}} catch (Exception e2) {}}}
}二、轉(zhuǎn)換流:字節(jié)流轉(zhuǎn)為字符流,作用是為了處理亂碼(編碼集、解碼集)。
1.編碼與解碼的概念:
解碼:二進(jìn)制(計(jì)算機(jī)只認(rèn)二進(jìn)制)解碼成字符(人只懂字符)。
編碼:字符編碼成二進(jìn)制。
文件都是二進(jìn)制,讀進(jìn)程序(轉(zhuǎn)成字符)就是解碼,寫(xiě)出去就是編碼(轉(zhuǎn)成二進(jìn)制寫(xiě)出到另一個(gè)文件)。
2.亂碼問(wèn)題:(解碼的時(shí)候要知道原先的編碼的字符集)
1)編碼與解碼字符集不統(tǒng)一。
2)字節(jié)缺少,長(zhǎng)度丟失。public class ConverDemo01 {public static void main(String[] args) throws UnsupportedEncodingException {test1();//解碼(把二進(jìn)制的中國(guó)轉(zhuǎn)成中國(guó)讓你看得懂,默認(rèn)gbk),byte->charString str ="中國(guó)";//編碼,字符轉(zhuǎn)字節(jié),char->bytebyte[] data =str.getBytes();//data=[-42, -48, -71, -6]//字節(jié)數(shù)不完整System.out.println(new String(data,0,3));//String(byte[] bytes, int offset, int length)通過(guò)byte數(shù)組構(gòu)建一個(gè)String,輸出中?
}/*** 編碼與解碼字符集必須相同,否則亂碼* @throws UnsupportedEncodingException */public static void test1() throws UnsupportedEncodingException{//解碼(把二進(jìn)制的中國(guó)轉(zhuǎn)成中國(guó)讓你看得懂,默認(rèn)gbk), byte -->charString str ="中國(guó)"; //gbk //編碼,字符轉(zhuǎn)字節(jié), char -->bytebyte[] data =str.getBytes();//data=[-42, -48, -71, -6]//編碼與解碼字符集同一System.out.println(new String(data));//中國(guó)data =str.getBytes("utf-8"); //設(shè)定編碼字符集,data=[-28, -72, -83, -27, -101, -67]//不同一出現(xiàn)亂碼System.out.println(new String(data));//涓浗//編碼byte[] data2 = "中國(guó)".getBytes("utf-8");//data2=[-28, -72, -83, -27, -101, -67]//解碼str=new String(data2,"utf-8");//中國(guó)System.out.println(str);}
}/*字節(jié)————(解碼)————>字符————(編碼)————>字節(jié)
源文件(二進(jìn)制文件,字節(jié))————(解碼)————>程序(在程序中為字符)————(編碼)————>目標(biāo)文件(二進(jìn)制文件)*/
/*** 轉(zhuǎn)換流: 只能字節(jié)轉(zhuǎn)為字符* 1、輸出流 OutputStreamWriter 編碼,* 2、輸入流 InputStreamReader 解碼,讀取是解碼(字節(jié)轉(zhuǎn)為字符),ANSI就是GBK的編碼方式。* 確保源不能為亂碼*/
public class ConverDemo02 {public static void main(String[] args) throws IOException {/*BufferedReader br =new BufferedReader(new FileReader(new File("E:/xp/test/Demo03.java")));//指定不了解碼的字符集,所以只能底層使用字節(jié)流,因?yàn)樽止?jié)給你可以解碼,字符給你不能解碼。*///指定解碼字符集BufferedReader br =new BufferedReader(//讀進(jìn)程序,1.java的文件的編碼使用UTF-8編碼的,所以這里的解碼要UTF-8。new InputStreamReader(//字符流和字節(jié)流不能直接操作,所以要用一個(gè)轉(zhuǎn)換流。new BufferedInputStream(new FileInputStream( new File("E:/xp/test/1.java"))),"UTF-8"));//寫(xiě)出文件 編碼BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(//用于編碼的轉(zhuǎn)換流new BufferedOutputStream( new FileOutputStream(new File("E:/xp/test/2.java")))));String info =null;while(null!=(info=br.readLine())){//讀取源文件,每次讀一行。System.out.println(info);bw.write(info);bw.newLine();}bw.flush();bw.close();br.close();}
}其他流(數(shù)據(jù)在網(wǎng)絡(luò)中傳輸都是通過(guò)流,不可能是傳字符串):
一、字節(jié)數(shù)組(其他電腦的內(nèi)存,服務(wù)器的內(nèi)存):
輸入流:ByteArrayInputStream read(byte[] byte int off,int len) + close()
輸出流:ByteArrayOutputStream write(byte[] byte int off,int len)/*** 字節(jié)數(shù)組 節(jié)點(diǎn)流* 數(shù)組的長(zhǎng)度有限 ,數(shù)據(jù)量不會(huì)很大* * 文件內(nèi)容不用太大* 1、文件 --程序->字節(jié)數(shù)組* 2、字節(jié)數(shù)組 --程序->文件*/
public class ByteArrayDemo01 {public static void main(String[] args) throws IOException {read(write()); }/*** 輸出流 操作與文件輸出流 有些不同, 有新增方法,不能使用多態(tài)* @throws IOException */public static byte[] write() throws IOException{//目的地,一個(gè)字節(jié)數(shù)組。byte[] dest;//選擇流 不同點(diǎn)ByteArrayOutputStream bos =new ByteArrayOutputStream();//操作 寫(xiě)出String msg ="操作與 文件輸入流操作一致";byte[] info =msg.getBytes();bos.write(info, 0, info.length);//寫(xiě)到bos這個(gè)管道里去了//獲取數(shù)據(jù)dest =bos.toByteArray();//釋放資源
bos.close();return dest;}/*** 輸入流 操作與 文件輸入流操作一致* 讀取字節(jié)數(shù)組(之前是讀文件),數(shù)組的長(zhǎng)度有限,數(shù)據(jù)量不會(huì)很大。* @throws IOException */public static void read(byte[] src) throws IOException{//數(shù)據(jù)源傳入 String msg = "輸入流 操作與 文件輸入流操作一致";///byte[] src = msg.getBytes();//選擇流InputStream is =new BufferedInputStream(new ByteArrayInputStream(src));//跟外界沒(méi)有聯(lián)系就不會(huì)有檢查異常//操作byte[] flush =new byte[1024];int len =0;while(-1!=(len=is.read(flush))){System.out.println(new String(flush,0,len));}//釋放資源is.close();}
}/***1、文件 --通過(guò)程序->字節(jié)數(shù)組*1)、文件輸入流 * 字節(jié)數(shù)組輸出流** 2、字節(jié)數(shù)組 --通過(guò)程序->文件* 1)、字節(jié)數(shù)組輸入流* 文件輸出流*/
public class ByteArrayDemo02 {public static void main(String[] args) throws IOException {byte[] data =getBytesFromFile("e:/xp/test/1.jpg");//文件 --通過(guò)程序->字節(jié)數(shù)組toFileFromByteArray(data,"e:/xp/test/arr.jpg");//字節(jié)數(shù)組 --通過(guò)程序->文件
}/*** 2、字節(jié)數(shù)組 --程序->文件*/public static void toFileFromByteArray(byte[] src,String destPath) throws IOException{//創(chuàng)建源src//目的地File dest=new File(destPath);//選擇流(不同的文件類型,選擇的流不一樣)//字節(jié)數(shù)組輸入流InputStream is =new BufferedInputStream(new ByteArrayInputStream(src)); //文件輸出流OutputStream os =new BufferedOutputStream(new FileOutputStream(dest));//操作 不斷讀取字節(jié)數(shù)組byte[] flush =new byte[1];int len =0;while(-1!=(len =is.read(flush))){//寫(xiě)出到文件中os.write(flush, 0, len);}os.flush();//釋放資源
os.close();is.close();}/*** 1、文件 --程序->字節(jié)數(shù)組*/public static byte[] getBytesFromFile(String srcPath) throws IOException{//創(chuàng)建文件源File src =new File(srcPath);//創(chuàng)建字節(jié)數(shù)組目的地 byte[] dest =null;//選擇流//文件輸入流 InputStream is =new BufferedInputStream(new FileInputStream(src));//字節(jié)數(shù)組輸出流 不能使用多態(tài)ByteArrayOutputStream bos =new ByteArrayOutputStream();//操作 不斷讀取文件 寫(xiě)出到字節(jié)數(shù)組流中byte[] flush =new byte[1024];int len =0;while(-1!=(len =is.read(flush))){//寫(xiě)出到字節(jié)數(shù)組流中bos.write(flush, 0, len);}bos.flush();//輸出流都要flush一下//獲取數(shù)據(jù)dest =bos.toByteArray();bos.close();is.close(); return dest;}
}其他流
二、處理流:
1.處理基本類型+字符串(保留數(shù)據(jù)和類型),是處理流就要借助于節(jié)點(diǎn)流(把東西存到哪個(gè)地方去),
輸入流:DataInputStream(看到InputStream就要字節(jié)流不能用字符流) readXxx
輸出流:DataOutputStream writeXxx/*** 數(shù)據(jù)類型(類型只能是基本類型+String)處理流* 1、輸入流 DataInputStream readXxx()* 2、輸出流 DataOutputStream writeXxx()* 新增方法不能使用多態(tài)* java.io.EOFException :沒(méi)有讀取到相關(guān)的內(nèi)容*/
public class DataDemo01 {public static void main(String[] args) {try {write("e:/xp/test/data.txt");//read("e:/xp/test/arr.txt"); //非法內(nèi)容read("e:/xp/test/data.txt");} catch (IOException e) {e.printStackTrace();}}/*** 從文件讀取數(shù)據(jù)+類型* @throws IOException */public static void read(String destPath) throws IOException{//創(chuàng)建源File src =new File(destPath);//選擇流DataInputStream dis =new DataInputStream(new BufferedInputStream(new FileInputStream(src)));//操作 讀取的順序與寫(xiě)出一致 必須存在才能讀取//讀取的順序不一致,數(shù)據(jù)輸出會(huì)存在問(wèn)題long num2 =dis.readLong();double num1 =dis.readDouble();String str =dis.readUTF();dis.close();System.out.println(num2+"-->"+str);}/*** 數(shù)據(jù)+類型輸出到文件* @throws IOException */public static void write(String destPath) throws IOException{double point =2.5;long num=100L;String str ="數(shù)據(jù)類型";//創(chuàng)建源File dest =new File(destPath);//選擇流 DataOutputStreamDataOutputStream dos =new DataOutputStream(new BufferedOutputStream(//OutputStream的子類new FileOutputStream(dest)));//操作 寫(xiě)出的順序 為讀取準(zhǔn)備,寫(xiě)入到文件data.txt
dos.writeDouble(point);dos.writeLong(num);dos.writeUTF(str); dos.flush();//釋放資源
dos.close();}
}/*** 數(shù)據(jù)類型(基本+String)處理流* 1、輸入流 DataInputStream readXxx()* 2、輸出流 DataOutputStream writeXxx()* 新增方法不能使用多態(tài)* java.io.EOFException :沒(méi)有讀取到相關(guān)的內(nèi)容*/
public class DataDemo02 {public static void main(String[] args) {try {byte[] data=write();read(data);System.out.println(data.length);} catch (IOException e) {e.printStackTrace();}}/*** 從字節(jié)數(shù)組讀取數(shù)據(jù)+類型* @throws IOException */public static void read(byte[] src) throws IOException{//字節(jié)數(shù)組,選擇流用字節(jié)數(shù)組輸入流DataInputStream dis =new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(src)));//操作 讀取的順序與寫(xiě)出一致 必須存在才能讀取double num1 =dis.readDouble();long num2 =dis.readLong();String str =dis.readUTF();dis.close();System.out.println(num1+"-->"+num2+"-->"+str);}/*** 數(shù)據(jù)+類型輸出到字節(jié)數(shù)組中,輸出到字節(jié)數(shù)組中用ByteArrayOutputStream。* @throws IOException */public static byte[] write() throws IOException{//目標(biāo)數(shù)組byte[] dest =null;double point =2.5;long num=100L;String str ="數(shù)據(jù)類型";//選擇流 ByteArrayOutputStream DataOutputStreamByteArrayOutputStream bos =new ByteArrayOutputStream();DataOutputStream dos =new DataOutputStream(new BufferedOutputStream(bos));//操作 寫(xiě)出的順序 為讀取準(zhǔn)備
dos.writeDouble(point);dos.writeLong(num);dos.writeUTF(str); dos.flush();dest =bos.toByteArray();//釋放資源
dos.close();return dest;//把double、long、String寫(xiě)入到字節(jié)數(shù)組dest中。
}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/yaowen/p/4833597.html
總結(jié)
以上是生活随笔為你收集整理的java13 InputStream,Reader的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: iOS高仿城觅应用客户端项目(开发思路和
- 下一篇: 概要设计说明书(转载)