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