NStorm
NStorm

Reputation: 2528

How to save the contents of a plain text file on the phones storage to a variable in Android?

I have some plain text data in the external storage of an android device and I want to save that plain text to a variable how could I do that?

I tried:

Scanner in;
try {
in = new Scanner(openFileInput("aspw.dat"));
String password = in.nextLine();
} catch (FileNotFoundException e1) {
errortoast.show();
e1.printStackTrace();
}

And I get a file not found exception even though the file aspw.dat is right there in the root of the SD card.

Upvotes: 0

Views: 185

Answers (1)

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Use this code:

Scanner in = new Scanner(openFileInput("Name of file"));
String stuff = in.nextLine();

stuff will then contain the contents of the first line in your file. You can keep calling in.nextLine() if there is supposed to be more than one line in the file. For more information on using scanners checkout the Scanner Documentation. For more on using android file input and output checkout the documentation for Android Data Storage.

Upvotes: 1

Related Questions