我有一個簡單的.txt文件,現在只有幾行,每行有一個單詞,然後是逗號,然後是另一個單詞,代表一個非常簡單的用戶名,密碼銀行。出於某種原因,雖然我不能讓文件打開來讀取它。未能打開文件,我連接錯了嗎?或者Android Studio由於某種原因沒有看到它?
這裏是我的代碼,我使用....
try {
final String PATH = "src\\main\\assets\\passwords.txt";
Log.w("myApp", "passed");
List<String> user_password = FileUtils.readLines(new File(PATH));
Log.w("myApp", "passed2");
@SuppressWarnings("unchecked") List<Credentials> credentials = (List<Credentials>) CollectionUtils.collect(user_password, new Transformer() {
@Override
public Object transform(Object input) {
String cred = (String) input;
String parsed[] = cred.split(",");
Log.w("myApp", parsed[0]);
return new Credentials(parsed[0], parsed[1]);
//return credential;
}
});
user = (Credentials) CollectionUtils.find(credentials, new Predicate() {
@Override
public boolean evaluate(Object object) {
Credentials c = (Credentials) object;
return c.getUserName().equals(userName);
}
});
} catch (IOException e) {
System.out.print(e);
Log.w("MyApp", "failed");
}
我試圖把password.txt的文件在不同的地方,但似乎並沒有任何工作。
謝謝...我做了'InputStream stream = this.getAssets()。open(「passwords.txt」);' 'BufferedReader reader = new BufferedReader(new inputStream(stream));' 'String line = reader.readLine();'它成功地打開並閱讀第一行如預期...是否有反正你知道使用FileUtils而不是BufferedReader? – John
@Shane:由於Android SDK中沒有'FileUtils',所以我無法真正幫助你,對不起。 – CommonsWare