Failing to open file, am I linking it wrong? Or is Android Studio not seeing it for some reason?

会有一股神秘感。 提交于 2020-03-16 10:08:51

问题


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");
}

I've tried putting the passwords.txt file in different places but that doesn't seem to work either.


回答1:


You're referencing wrong to file in assets folder. It has to be smth like:

file:///android_asset/myfoldername/myfilename

in your particular case it's file:///android_asset/passwords.txt, though you have to keep in mind that it's always read only file




回答2:


final String PATH = "src\\main\\assets\\passwords.txt";

That's not going to work. Android is not Windows, and an Android device is not your Windows development PC.

First, \ is the Windows path separator. On OS X, Linux, and Android, the path separator is /.

Second, src\main\assets\passwords.txt is a file in your project. It is not a file on the filesystem of the Android device.

To access assets, use AssetManager to open an InputStream(). You can get an AssetManager by calling getAssets() on any handy Context, such as your activity. Then, for your asset, call open("passwords.txt") on the AssetManager to get the InputStream, that you can then use to read in the data.




回答3:


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);
    }



回答4:


Kotlin: You can easily retrive file from assets using AssetsManager and InputStream like below-

 val  assetManager: AssetManager = assets
 val  inputStream = assetManager.open("file_name_with_extention")

For retrieve file like Image:

val drawable = Drawable.createFromStream(inputStream, null)


来源:https://stackoverflow.com/questions/34708273/failing-to-open-file-am-i-linking-it-wrong-or-is-android-studio-not-seeing-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!