无法打开文件,是否链接错误?还是Android Studio因为某种原因没有看到它?
我有一个简单的.txt文件,现在只有几行,每行有一个单词,然后是逗号,然后是另一个单词,代表一个非常简单的用户名,密码银行。由于某种原因,虽然我无法打开文件来读取它 这是我正在使用的代码无法打开文件,是否链接错误?还是Android Studio因为某种原因没有看到它?,android,android-studio,Android,Android Studio,我有一个简单的.txt文件,现在只有几行,每行有一个单词,然后是逗号,然后是另一个单词,代表一个非常简单的用户名,密码银行。由于某种原因,虽然我无法打开文件来读取它 这是我正在使用的代码 try { final String PATH = "src\\main\\assets\\passwords.txt"; Log.w("myApp", "passed"); List<String> user_password = FileUtils.readLines(n
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");
}
试试看{
最终字符串路径=“src\\main\\assets\\passwords.txt”;
Log.w(“myApp”,“通过”);
列出用户_password=FileUtils.readLines(新文件(路径));
Log.w(“myApp”、“passed2”);
@SuppressWarnings(“未选中”)列表凭据=(列表)CollectionUtils.collect(用户密码,新转换器(){
@凌驾
公共对象变换(对象输入){
字符串cred=(字符串)输入;
已分析的字符串[]=cred.split(“,”);
Log.w(“myApp”,已解析[0]);
返回新凭据(已解析[0],已解析[1]);
//返回凭证;
}
});
user=(凭据)CollectionUtils.find(凭据,新谓词(){
@凌驾
公共布尔求值(对象){
凭证c=(凭证)对象;
返回c.getUserName().equals(用户名);
}
});
}捕获(IOE异常){
系统输出打印(e);
Log.w(“MyApp”,“失败”);
}
我试着把passwords.txt文件放在不同的地方,但这似乎也不起作用
您对资产文件夹中的文件引用错误。它必须是smth,如:
file:///android_asset/myfoldername/myfilename
在您的特定情况下,它是file:///android_asset/passwords.txt
,但您必须记住,它始终是只读文件
final String PATH = "src\\main\\assets\\passwords.txt";
那是行不通的。Android不是Windows,Android设备也不是您的Windows开发PC
首先,\
是Windows路径分隔符。在OSX、Linux和Android上,路径分隔符是/
其次,src\main\assets\passwords.txt
是项目中的一个文件。它不是安卓设备文件系统中的文件
要访问资产,请使用
AssetManager
打开InputStream()
。您可以通过在任何方便的上下文
上调用getAssets()
来获取AssetManager
,例如您的活动。然后,对于您的资产,在AssetManager
上调用open(“passwords.txt”)
,以获取InputStream
,然后您可以使用它读取数据。多亏了@commonware,我才能够通过使用InputStream实现我试图做的事情,然后使用IOUtils将所有内容读取到列表中
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);
}
试试看{
InputStream iS=this.getAssets().open(“passwords.txt”);
列出用户\密码=IOUtils.readLines(iS);
@SuppressWarnings(“未选中”)列表凭据=(列表)CollectionUtils.collect(用户密码,新转换器(){
@凌驾
公共对象变换(对象输入){
字符串cred=(字符串)输入;
已分析的字符串[]=cred.split(“,”);
返回新凭据(已解析[0],已解析[1]);
}
});
user=(凭据)CollectionUtils.find(凭据,新谓词(){
@凌驾
公共布尔求值(对象){
凭证c=(凭证)对象;
返回c.getUserName().equals(用户名);
}
});
}捕获(IOE异常){
系统输出打印(e);
}
谢谢。。。我做了“InputStream=this.getAssets().open”(“passwords.txt”);”BufferedReader reader=new BufferedReader(new inputStream(stream));“”字符串行=reader.readLine();'已按预期成功打开并读取第一行。。。你知道用FileUtils代替BufferedReader吗?@Shane:因为Android SDK中没有FileUtils
,我真的不能帮你,对不起。