我有一個基於FileInputStream的函數,該函數應返回從文件中讀取的字符串。但爲什麼返回值是空的。 函數的返回值應該顯示在MainActivity中。 此功能是我的應用程序中的服務的一部分。如何在Android中返回FileInputStream的值
public static String UserLoginFile;
public static String UserPassFile;
public String LoadUserInfopassFromFilePostData() {
String FILENAME = "TeleportSAASPass.txt";
FileInputStream inputStream = null;
if(UserLoginFile != null){
try {
inputStream = openFileInput(FILENAME);
byte[] reader = new byte[inputStream.available()];
while (inputStream.read(reader) != -1) {}
UserPassFile = new String(reader);
// Toast.makeText(getApplicationContext(), "GPSTracker " + UserPassFile, Toast.LENGTH_LONG).show();
} catch(IOException e) {
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
return UserPassFile;
}
請告訴我如何解決我的功能,以便它可以返回她從文件中讀取的字符串。
你是否確信你沒有捕捉任何異常?您也可以嘗試DataInputStream並執行readLine() – britzl