Monday, 20 July 2015

Java: Read file into a string

Following snippet read file content as a string.

public static String readFileContent(String fileName) throws IOException {
         return new String(Files.readAllBytes(Paths.get(fileName)));

}

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileReader {

 public static String readFileContent(String fileName) throws IOException {
  return new String(Files.readAllBytes(Paths.get(fileName)));
 }

 public static void main(String args[]) throws IOException {
  String content = readFileContent("/Users/harikrishna_gurram/emp.txt");
  System.out.println(content);
 }
}



No comments:

Post a Comment