0
我正在使用下一個方法從文件中讀取內容。這裏的問題是,我只限於指定爲inputBuffer
(在本例中爲1024)的字符數。首先,如果內容長度少於1024個字符,我會得到很多空白字符,而我需要使用修剪來刪除它們。Android。從文件中讀取內容
其次,更重要的是,即使文件超過1024個字符並將其插入到String
對象中,我也要閱讀文件的全部內容。我知道我不應該使用.available
方法來確定文件中是否有更多的數據,因爲它不準確或類似的東西。
關於如何去做這件事的任何想法?
public String getContent(String sFileName)
{
//Stop in case the file does not exists
if (!this.exists(sFileName))
return null;
FileInputStream fIn = null;
InputStreamReader isr = null;
String data = null;
try{
char[] inputBuffer = new char[1024];
fIn = _context.openFileInput(sFileName);
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fIn.close();
}catch(IOException e){
e.printStackTrace(System.err);
return null;
}
return data.trim();
}
hmm。世界上怎麼沒有我想到的。是的,你在那裏保羅。 – AndreiBogdan
你有任何爭論,爲什麼這個解決方案可能比我逐行閱讀的更好或者更差?或者任何方法都很好? – AndreiBogdan
嗯,似乎分配緩衝區,它不接受新的char [long],只有char [int]。那麼,這是一個無賴 – AndreiBogdan