Read file to string


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private String readFile(String pathname) throws IOException {
  2. StringBuilder stringBuilder = new StringBuilder();
  3. Scanner scanner = new Scanner(new File(pathname));
  4.  
  5. try {
  6. while(scanner.hasNextLine()) {
  7. stringBuilder.append(scanner.nextLine() + "\n");
  8. }
  9. } finally {
  10. scanner.close();
  11. }
  12. return stringBuilder.toString();
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.