Java的知识点29—— 文件字符流、字节数组流
生活随笔
收集整理的這篇文章主要介紹了
Java的知识点29—— 文件字符流、字节数组流
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?文件字符流 FileReader? FileWriter
分段讀取 文件字符輸入流
package cn.dym; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; /*** 四個步驟: 分段讀取 文件字符輸入流* 1、創(chuàng)建源* 2、選擇流* 3、操作* 4、釋放資源* @author**/ public class IOTest505 {public static void main(String[] args) {//1、創(chuàng)建源File src = new File("abc.txt");//2、選擇流Reader reader =null;try {reader =new FileReader(src);//3、操作 (分段讀取)char[] flush = new char[1024]; //緩沖容器int len = -1; //接收長度while((len=reader.read(flush))!=-1) {//字符數(shù)組-->字符串String str = new String(flush,0,len);System.out.println(str);} } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {//4、釋放資源try {if(null!=reader) {reader.close();}} catch (IOException e) {e.printStackTrace();}}}}文件字符輸出流
package cn.dym; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.Writer;/*** 四個步驟: 文件字符輸出流* 1、創(chuàng)建源* 2、選擇流* 3、操作* 4、釋放資源* @author **/ public class IOTest06 {public static void main(String[] args) {//1、創(chuàng)建源File dest= new File("dest.txt");//2、選擇流Writer writer =null;try {writer =new FileWriter(dest);//3、操作 (分段讀取)//寫法一 // String msg="IO is so easy\r\n代止兮歡迎你"; // char[] datas=msg.toCharArray();//字符串——》字符數(shù)組 // writer.write(datas, 0, datas.length); // writer.flush(); //寫法二 // String msg="IO is so easy\r\n代止兮歡迎你"; // writer.write(msg); //字符也是字符數(shù)組 // writer.flush(); //寫法三writer.append("IO is so easy\r\n").append("哈哈哈\r\n").append("代止兮歡迎你");writer.flush(); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {//4、釋放資源try {if(null!=writer) {writer.close();}} catch (IOException e) {e.printStackTrace();}}} }字節(jié)數(shù)組流
字節(jié)數(shù)組輸入流
package cn.dym;import java.io.ByteArrayInputStream; import java.io.InputStream;/*** 四個步驟: 字節(jié)數(shù)組輸入流* 1、創(chuàng)建源 : 字節(jié)數(shù)組 不要太大* 2、選擇流* 3、操作* 4、釋放資源: 可以不用處理* @author **/ public class IOTest07 {public static void main(String[] args) {//1、創(chuàng)建源byte [] src="talk is cheap show me the code".getBytes();//2、選擇流InputStream is=null;is=new ByteArrayInputStream(src);} }字節(jié)數(shù)組輸出流 ByteArrayOutputStream
package cn.dym;import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException;/*** 字節(jié)數(shù)組輸出流 ByteArrayOutputStream*1、創(chuàng)建源 : 內(nèi)部維護*2、選擇流 : 不關(guān)聯(lián)源*3、操作(寫出內(nèi)容)*4、釋放資源 :可以不用** 獲取數(shù)據(jù): toByteArray()* */ public class IOTest07 {public static void main(String[] args) {//1、創(chuàng)建源byte[] dest =null;//2、選擇流 (新增方法)ByteArrayOutputStream baos =null;try {baos = new ByteArrayOutputStream();//3、操作(寫出)String msg ="show me the code";byte[] datas =msg.getBytes(); // 字符串-->字節(jié)數(shù)組(編碼)baos.write(datas,0,datas.length);baos.flush();//獲取數(shù)據(jù)dest = baos.toByteArray();System.out.println(dest.length +"-->"+new String(dest,0,baos.size()));}catch(FileNotFoundException e) { e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{//4、釋放資源try {if (null != baos) {baos.close();} } catch (Exception e) {}}}}IO_綜合_對接流實例
package cn.dym;import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;/*** 1. 圖片讀取到字節(jié)數(shù)組中* 2. 字節(jié)數(shù)組寫出到文件* 獲取數(shù)據(jù): toByteArray()* */ public class IOTest08 {public static void main(String[] args) {//圖片轉(zhuǎn)成字節(jié)數(shù)組byte[] datas = fileToByteArray("p.png");System.out.println(datas.length);byteArrayToFile(datas,"p-byte.png");}// 1. 圖片讀取到字節(jié)數(shù)組中// 1). 圖片到程序 FileInputStream// 2). 程序到字節(jié)數(shù)組 ByteArrayOutputStreampublic static byte[] fileToByteArray(String filePath) {//1、創(chuàng)建源與目的地File src = new File(filePath);byte[] dest =null;//2、選擇流InputStream is =null;ByteArrayOutputStream baos =null;try {is =new FileInputStream(src);baos = new ByteArrayOutputStream();//3、操作 (分段讀取)byte[] flush = new byte[1024*10]; //緩沖容器int len = -1; //接收長度while((len=is.read(flush))!=-1) {baos.write(flush,0,len); //寫出到字節(jié)數(shù)組中 } baos.flush();return baos.toByteArray();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {//4、釋放資源try {if(null!=is) {is.close();}} catch (IOException e) {e.printStackTrace();}}return null; }/*** 2、字節(jié)數(shù)組寫出到圖片* 1)、字節(jié)數(shù)組到程序 ByteArrayInputStream* 2)、程序到文件 FileOutputStream*/public static void byteArrayToFile(byte[] src,String filePath) {//1、創(chuàng)建源File dest = new File(filePath);//2、選擇流InputStream is =null;OutputStream os =null;try {is =new ByteArrayInputStream(src);os = new FileOutputStream(dest);//3、操作 (分段讀取)byte[] flush = new byte[5]; //緩沖容器int len = -1; //接收長度while((len=is.read(flush))!=-1) {os.write(flush,0,len); //寫出到文件 } os.flush();} catch (IOException e) {e.printStackTrace();}finally {//4、釋放資源try {if (null != os) {os.close();} } catch (Exception e) {}}} }?
總結(jié)
以上是生活随笔為你收集整理的Java的知识点29—— 文件字符流、字节数组流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java的知识点28——文件编码、IO流
- 下一篇: java的知识点30——设计模式