Skip to content

Instantly share code, notes, and snippets.

@l4p4
Last active August 29, 2015 14:19
Show Gist options
  • Save l4p4/64ff4ca470678e185381 to your computer and use it in GitHub Desktop.
Save l4p4/64ff4ca470678e185381 to your computer and use it in GitHub Desktop.
ReadFile with JAVA 1.8
package java18;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ReaderFile {
private static StringBuilder sb;
static {
sb = new StringBuilder();
}
public static void main(String[] args) {
read(Paths.get("file.in"));
}
public final static void read(Path path) {
try (BufferedReader rd = Files.newBufferedReader(path, Charset.forName("UTF-8"))) {
while (sb.append(rd.readLine()).append(System.getProperty("line.separator")) != null
&& rd.ready())
;
} catch (NoSuchFileException e) {
System.err.println(String.format("File not exists: %s", e.getMessage()));
} catch (IOException e) {
System.err.println(e);
} finally {
System.out.println(sb);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment