Created
August 4, 2017 09:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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