Skip to content

Instantly share code, notes, and snippets.

@francisnnumbi
Created August 4, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisnnumbi/1b062563b9a82b882791790478ca1472 to your computer and use it in GitHub Desktop.
Save francisnnumbi/1b062563b9a82b882791790478ca1472 to your computer and use it in GitHub Desktop.
Simplest way of reading plain text from external file using Scanner.class. Remember to include this permission in your manifest file: READ_EXTERNAL_STORAGE but if you got write permission, it is not necessary to include read permission as it is already incorporated in write permission.
public String readText(File file){
StringBuilder builder = new StringBuilder();
try{
Scanner scanner = new Scanner(file);
while(scanner.hasNext()){
builder.append(scanner.nextLine());
builder.append('\n');
}
scanner.close();
}
return builder.toString().trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment