Java中經常使用的字節流和字符流

IO流(輸入流、輸出流) 字節流、字符流   1.字節流:java

InputStream、OutputStream InputStream抽象了應用程序讀取數據的方式; OutputStream抽象了應用程序寫出數據的方式;    2.EOF=End 讀到-1就讀到結尾sql

  3.輸入流的基本方法:數組

int b=in.read();讀取一個字節無符號填充到int低八位;-1是EOF; in.read(byte[] buf) in.read(byte[] buf,int start,int size)   4.輸出流基本方法: 架構

out.write(int b) 寫出一個byte到流,b的低8位; out.write(byte[] buf) 將buf字節數組都寫入到流; out.write(byte[] buf,int start,int size)          5.FileInputStream--->具體實現了在文件上讀取數據,下面請看最簡單的文件讀取:併發

複製代碼 package com.wxd.test2;分佈式

import java.io.FileInputStream; import java.io.IOException;高併發

public class IOUtil { /** * 讀取指定文件內容,按照16進制輸出到控制檯 * 而且每輸出10個byte換行 * @param fileName */ public static void printHex(String fileName) throws IOException { //把文件做爲字節流進行讀操做 FileInputStream in =new FileInputStream(fileName); int b; int i=1; while ((b=in.read())!=-1){ if(b<=0xf){ //單位數前面補0 System.out.print(0); } System.out.print(Integer.toHexString(b)+" "); if(i++%10==0){ System.out.println(); } } in.close(); } } 複製代碼性能

  6.FileOutputStream實現了向文件中寫出byte數據的方法:學習

複製代碼 package com.wxd.test2;編碼

import java.io.FileOutputStream; import java.io.IOException;

public class FileOutDemo1 { public static void main(String[] args) throws IOException{ //若是該文件不存在則直接建立,若是存在刪除後建立 FileOutputStream out=new FileOutputStream("demo/out.dat"); out.write('A');//寫出了‘A’字符的低八位 out.write('B');//寫出了‘B’字符的低八位 int a=10;//write只能寫低八位寫一個int須要寫四次每次8位 out.write(a>>>24); out.write(a>>>16); out.write(a>>>8); out.write(a); byte[] zg="中國".getBytes("UTF-8"); out.write(zg); out.close();

IOUtil.printHex("demo/out.dat");

}
複製代碼

} 複製代碼   7.DataOutputStream/DataInputStream對「流」功能的擴展,能夠更加方便的讀取int,long,字符等類型數據:

writeInt()/writeDouble()/writeUTF()  

複製代碼 package com.wxd.test2;

import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException;

public class DosDemo { public static void main(String[] args) throws IOException { String file="demo/dos.dat"; DataOutputStream dos=new DataOutputStream(new FileOutputStream(file)); dos.writeInt(10); dos.writeInt(-10); dos.writeLong(10l); dos.writeDouble(10.5); //採用UTF-8編碼寫出 dos.writeUTF("中國"); //採用utf-16be編碼寫出 dos.writeChars("中國"); dos.close(); } } 複製代碼

複製代碼 package com.wxd.test2;

import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException;

public class DisDemo { public static void main(String[] args) throws IOException { String file="demo/dos.dat"; DataInputStream dis=new DataInputStream(new FileInputStream(file)); int i=dis.readInt();//其實作了4次read,由於int是32位 System.out.println(i); i=dis.readInt(); System.out.println(i); long l=dis.readLong();//其實作了8次read,應爲long是64位 System.out.println(l); double d=dis.readDouble(); System.out.println(d); String s=dis.readUTF(); System.out.println(s); dis.close(); } } 複製代碼   7.BufferedInputStream&BufferedOutputStrean這兩個流爲IO提供了帶緩衝區的操做,通常打開文件進行寫入或讀取操做時,都會加上緩衝,這種流模式提升了IO性能

從應用程序中把數據放入文件,至關於將一缸水倒入到另外一個缸中:

FileOutputStream--->write()方法至關於一滴一滴地把水轉移過去 DataOutputStream--->writeXxx()方法會方便一些,至關於一瓢一瓢的把水轉移過去 BufferedOutputStream--->write()方法至關於一瓢一瓢先放入桶中,再從桶中倒入缸中 複製代碼 /*** *進行文件的拷貝,利用帶緩衝的字節流 * @param srcFile * @param destFile * @throws IOException */ public static void copyFileByBuffer(File srcFile,File destFile) throws IOException{ if(!srcFile.exists()){ throw new IllegalArgumentException("文件:"+srcFile+"不存在"); } if(!srcFile.isFile()){ throw new IllegalArgumentException(srcFile+"不是文件!"); } BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); int c; while ((c=bis.read())!=-1){ bos.write(c); bos.flush();//刷新緩衝區,不然寫入不到; } bis.close(); bos.close(); } 複製代碼 字符流 1.編碼問題

2.認識文本和文本文件

3.Java的文本(char)是16位無符號的整數,是字符的unicode編碼(雙字節編碼)文件是byte byte byte...的數據序列

  文本文件是文本(char)序列按照某種編碼方案(utf-8,utf-16be,gbk)序列化爲byte的存儲

4.字符流(Reader Writer):--->操做的是文本文件

  字符的處理,一次處理一個字符

  字符的底層任然是基本的字節序列

  字符流的基本實現

    InputStreamReader 完成byte流解析爲char流,按照編碼解析

    OutputStreamWriter 提供char流到byte流,按照編碼處理

複製代碼 package com.wxd.test2;

import java.io.*;

public class IsrAndOswDemo { public static void main(String[] args) throws IOException{ //InputStreamReader被稱爲橋樑流 FileInputStream in=new FileInputStream("demo\test.txt");//默認項目的編碼,操做的時候要寫文件自己的編碼 InputStreamReader isr=new InputStreamReader(in,"utf-8"); FileOutputStream out=new FileOutputStream("demo\test2.txt"); OutputStreamWriter osw=new OutputStreamWriter(out,"utf-8"); // int c; // while ((c=isr.read())!=-1){ // System.out.print((char)c); // } char[] buffer=new char[8*1024]; int c; //批量讀取,放入buffer這個字符數組,從第0個位置開始放置,最多放buffe.length個 //返回的是讀到的字符的個數 while ((c=isr.read(buffer,0,buffer.length))!=-1){ String s=new String(buffer,0,c); System.out.println(s); osw.write(buffer,0,c); osw.flush(); } isr.close(); osw.close(); } } 複製代碼

5.FileReader/FileWriter(這種方式bu)

複製代碼 package com.wxd.test2;

import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;

public class FrAndFwDemo { public static void main(String[] args) throws IOException{ FileReader fr=new FileReader("demo\test.txt"); FileWriter fw=new FileWriter("demo\text2.txt",true);//設置爲true在後面追加 char[] buffer=new char[2056]; int c; while ((c=fr.read(buffer,0,buffer.length))!=-1){ fw.write(buffer,0,c); fw.flush(); } fr.close(); fw.close(); } } 複製代碼 6.字符流的過濾器

  BufferedReader ----->readLine一次讀一行

  BufferedWriter/PrintWriter ---->寫一行

複製代碼 package com.wxd.test2;

import java.io.*;

public class BrAndBwOrPwDemo { public static void main(String[] args) throws IOException { //對文件進行讀寫操做 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("demo\test.txt"))); /BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("demo\test3.txt")));/ PrintWriter pw = new PrintWriter("demo\test3.txt"); String line; while ((line = br.readLine()) != null) { System.out.println(line);//一次讀一行,並不能識別換行 /bw.write(line); //單獨寫出換行操做 bw.newLine(); bw.flush();/ pw.println(line); pw.flush(); } br.close(); // bw.close(); pw.close(); } 歡迎工做一到五年的Java工程師朋友們加入Java羣: 891219277 羣內提供免費的Java架構學習資料(裏面有高可用、高併發、高性能及分佈式、Jvm性能調優、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)合理利用本身每一分每一秒的時間來學習提高本身,不要再用"沒有時間「來掩飾本身思想上的懶惰!趁年輕,使勁拼,給將來的本身一個交代!

相關文章
相關標籤/搜索