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

后端 未结 3 426
忘了有多久
忘了有多久 2021-01-27 12:09

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. Fo

3条回答
  •  生来不讨喜
    2021-01-27 12:59

    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.

提交回复
热议问题