這篇文章給大家介紹使用Java怎么將讀取的文件轉(zhuǎn)換為字符串,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
成都創(chuàng)新互聯(lián)公司基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供四川主機(jī)托管 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。
方式一
/**
以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
當(dāng)然也是可以讀字符串的。
*/
/* 貌似是說(shuō)網(wǎng)絡(luò)環(huán)境中比較復(fù)雜,每次傳過(guò)來(lái)的字符是定長(zhǎng)的,用這種方式?*/ public String readString1() { try { //FileInputStream 用于讀取諸如圖像數(shù)據(jù)之類(lèi)的原始字節(jié)流。要讀取字符流,請(qǐng)考慮使用 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得大小取出來(lái)呢?為什么還要寫(xiě)入到bos中呢? return new(buffer,"UTF-8") 不更好么? // return new String(bos.toByteArray(),"UTF-8"); } }
方式二
// 有人說(shuō)了 FileReader 讀字符串更好,那么就用FileReader吧
// 每次讀一個(gè)是不是效率有點(diǎn)低了? 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出錯(cuò)"); } return str.toString(); }
方式三
/按字節(jié)讀取字符串/
/* 個(gè)人感覺(jué)最好的方式,(一次讀完)讀字節(jié)就讀字節(jié)吧,讀完轉(zhuǎn)碼一次不就好了*/
private static String readString3()
{
String str=""; File file=new File(FILE_IN); try { FileInputStream in=new FileInputStream(file); // size 為字串的長(zhǎng)度 ,這里一次性讀完 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類(lèi)是從字節(jié)流到字符流的橋梁/
/* 按行讀對(duì)于要處理的格式化數(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) // 處理?yè)Q行符的問(wèn)題 { 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(); }
關(guān)于使用Java怎么將讀取的文件轉(zhuǎn)換為字符串就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章標(biāo)題:使用Java怎么將讀取的文件轉(zhuǎn)換為字符串
轉(zhuǎn)載來(lái)于:http://aaarwkj.com/article2/gghooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、建站公司、微信小程序、網(wǎng)站內(nèi)鏈、網(wǎng)站維護(hù)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)