zabware zabware - 1 year ago 114
Java Question

How to get file content in java?

to get the content of a txt file I usually use a scanner and iterate over each line to get the content:

Scanner sc = new Scanner(new File("file.txt"));
String str = sc.nextLine();

Does the java api provide a way to get the content with one line of code like:

String content = FileUtils.readFileToString(new File("file.txt"))

Answer Source

With Java 7 there is an API along those lines.

Files.readAllLines(Path path, Charset cs)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download