Java中讀取文件轉(zhuǎn)換為字符串的方法
方式一
/**
以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
當(dāng)然也是可以讀字符串的。
*/
/* 貌似是說網(wǎng)絡(luò)環(huán)境中比較復(fù)雜,每次傳過來的字符是定長的,用這種方式?*/ public String readString1() { try { //FileInputStream 用于讀取諸如圖像數(shù)據(jù)之類的原始字節(jié)流。要讀取字符流,請考慮使用 FileReader。 FileInputStream inStream=this.openFileInput(FILE_NAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer=new byte[1024]; int length=-1; while( (length = inStream.read(buffer) != -1) { bos.write(buffer,0,length); // .write方法 SDK 的解釋是 Writes count bytes from the byte array buffer starting at offset index to this stream. // 當(dāng)流關(guān)閉以后內(nèi)容依然存在 } bos.close(); inStream.close(); return bos.toString(); // 為什么不一次性把buffer得大小取出來呢?為什么還要寫入到bos中呢? return new(buffer,"UTF-8") 不更好么? // return new String(bos.toByteArray(),"UTF-8"); } }
方式二
// 有人說了 FileReader 讀字符串更好,那么就用FileReader吧
// 每次讀一個是不是效率有點低了? private static String readString2() { StringBuffer str=new StringBuffer(""); File file=new File(FILE_IN); try { FileReader fr=new FileReader(file); int ch = 0; while((ch = fr.read())!=-1 ) { System.out.print((char)ch+" "); } fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("File reader出錯"); } return str.toString(); }
方式三
/按字節(jié)讀取字符串/
/* 個人感覺最好的方式,(一次讀完)讀字節(jié)就讀字節(jié)吧,讀完轉(zhuǎn)碼一次不就好了*/
private static String readString3()
{
String str=""; File file=new File(FILE_IN); try { FileInputStream in=new FileInputStream(file); // size 為字串的長度 ,這里一次性讀完 int size=in.available(); byte[] buffer=new byte[size]; in.read(buffer); in.close(); str=new String(buffer,"GB2312"); } catch (IOException e) { // TODO Auto-generated catch block return null; e.printStackTrace(); } return str;
}
方式四
/InputStreamReader+BufferedReader讀取字符串 , InputStreamReader類是從字節(jié)流到字符流的橋梁/
/* 按行讀對于要處理的格式化數(shù)據(jù)是一種讀取的好方式 */ private static String readString4() { int len=0; StringBuffer str=new StringBuffer(""); File file=new File(FILE_IN); try { FileInputStream is=new FileInputStream(file); InputStreamReader isr= new InputStreamReader(is); BufferedReader in= new BufferedReader(isr); String line=null; while( (line=in.readLine())!=null ) { if(len != 0) // 處理換行符的問題 { str.append("\r\n"+line); } else { str.append(line); } len++; } in.close(); is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str.toString(); }
路要一步一步走,記住自己走過的路,不再犯同樣的錯誤,才是真正的成長!歡迎指點、交流。
以上這篇Java中讀取文件轉(zhuǎn)換為字符串的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Struts2之Action接收請求參數(shù)和攔截器詳解
這篇文章主要介紹了Struts2之Action接收請求參數(shù)和攔截器詳解,非常具有實用價值,需要的朋友可以參考下2017-05-05Java synchronized關(guān)鍵_動力節(jié)點Java學(xué)院整理
在java中,每一個對象有且僅有一個同步鎖。這也意味著,同步鎖是依賴于對象而存在。下面通過本文給大家介紹synchronized原理 及基本規(guī)則,感興趣的朋友一起學(xué)習(xí)吧2017-05-05Java 線程對比(Thread,Runnable,Callable)實例詳解
這篇文章主要介紹了Java 線程(Thread,Runnable,Callable)實例詳解的相關(guān)資料,這里對java 線程的三種方法進(jìn)行了對比,需要的朋友可以參考下2016-12-12淺談Spring框架中@Autowired和@Resource的區(qū)別
最近review別人代碼的時候,看到了一些@Autowired不一樣的用法,覺得有些意思,下面這篇文章主要給大家介紹了關(guān)于Spring框架中@Autowired和@Resource區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-10-10springboot2.x默認(rèn)使用的代理是cglib代理操作
這篇文章主要介紹了springboot2.x默認(rèn)使用的代理是cglib代理操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringCloud如何創(chuàng)建一個服務(wù)提供者provider
這篇文章主要介紹了SpringCloud如何創(chuàng)建一個服務(wù)提供者provider,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07圖解Springboot集成七牛云并實現(xiàn)圖片上傳功能過程
在實際開發(fā)中 ,基本都會有應(yīng)用到文件上傳的場景,但隨著或多或少的需求問題,之前有在springboot上用過七牛云實現(xiàn)圖片上傳,今天因為某些原因又重新使用了下七牛云因此想總結(jié)下七牛云2021-11-11