无法打开文件,我链接错误吗?还是Android Studio由于某种原因没有看到它? [英] Failing to open file, am I linking it wrong? Or is Android Studio not seeing it for some reason?
问题描述
我有一个简单的.txt文件,现在只有几行,每行包含一个单词,然后是一个逗号,然后是另一个单词,代表一个非常简单的用户名,密码库.由于某种原因,尽管我无法打开该文件以从中读取文件.
I have a simple .txt file with just a couple lines in right now, each line has a word then a comma then another word, representing a very simplistic username , password bank. For some reason though I cant get the File to open to read from it.
这是我正在使用的代码....
Here is my code that I'm using....
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");
}
我尝试将 passwords.txt 文件放在不同的位置,但这似乎也不起作用.
I've tried putting the passwords.txt file in different places but that doesn't seem to work either.
推荐答案
感谢@CommonsWare,我能够通过使用InputStream和IOUtils将所有内容读取到列表中来实现我想做的事情.
Thanks to @CommonsWare I was able to achieve what I was trying to do by using InputStream and then also IOUtils to read everything into the List.
try {
InputStream iS = this.getAssets().open("passwords.txt");
List<String> user_password = IOUtils.readLines(iS);
@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(",");
return new Credentials(parsed[0], parsed[1]);
}
});
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);
}
这篇关于无法打开文件,我链接错误吗?还是Android Studio由于某种原因没有看到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!