我正在從文件中檢索數據,並且由於某種原因,我每次都會錯過第一個字符。我的代碼是 。閱讀文件時缺少第一個字母
public String readFile(){
String str = "Not Authenticated";
//Reading the file
try{
FileInputStream fIn = openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[isr.read()]; //str.length()
// Fill the Buffer with data from the file
try {
isr.read(inputBuffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
// Transform the chars to a String
String readString = new String(inputBuffer);
str = readString;
} catch (IOException ioe)
{return ioe.toString();}
return str;
}
文件中包含我得到「芸香」 還當我創建該文件的第一個字母不能是大寫的單詞「真」 ?如果我使用大寫的文件是從來沒有找到我猜這兩個不相關。
本文件中包含什麼?只是字符序列爲「真」,還是以一個字節的長度信息開始? –
方法'isr.read()'讀取第一個字符。 – adatapost
只是「真」這個詞,它是一個基本的文本文件。 – John