Java中讀取文件轉(zhuǎn)換為字符串的方法
方式一
/**
以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
當(dāng)然也是可以讀字符串的。
*/
/* 貌似是說(shuō)網(wǎng)絡(luò)環(huán)境中比較復(fù)雜,每次傳過(guò)來(lái)的字符是定長(zhǎng)的,用這種方式?*/
public String readString1()
{
try
{
//FileInputStream 用于讀取諸如圖像數(shù)據(jù)之類的原始字節(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)呢?為什么還要寫入到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è)人感覺最好的方式,(一次讀完)讀字節(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類是從字節(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ò)的路,不再犯同樣的錯(cuò)誤,才是真正的成長(zhǎng)!歡迎指點(diǎn)、交流。
以上這篇Java中讀取文件轉(zhuǎn)換為字符串的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用EasyPoi輕松導(dǎo)入導(dǎo)出Excel文檔的方法示例
這篇文章主要介紹了使用EasyPoi輕松導(dǎo)入導(dǎo)出Excel文檔的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例
這篇文章主要介紹了SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java Callable接口實(shí)現(xiàn)細(xì)節(jié)詳解
這篇文章主要介紹了Java Callable接口實(shí)現(xiàn)細(xì)節(jié)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Java并發(fā)系列之AbstractQueuedSynchronizer源碼分析(條件隊(duì)列)
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之AbstractQueuedSynchronizer源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
SpringBoot通過(guò)token實(shí)現(xiàn)用戶互踢功能(具體實(shí)現(xiàn))
所謂token,既用戶能夠在一定時(shí)間內(nèi)證明自己身份的一長(zhǎng)串字符串,這篇文章主要介紹了SpringBoot通過(guò)token實(shí)現(xiàn)用戶互踢功能,需要的朋友可以參考下2024-04-04
springboot如何獲取接口下所有實(shí)現(xiàn)類
這篇文章主要介紹了springboot如何獲取接口下所有實(shí)現(xiàn)類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Spring Cloud超詳細(xì)i講解Feign自定義配置與使用
這篇文章主要介紹了SpringCloud Feign自定義配置與使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

