Last active
August 29, 2015 14:19
-
-
Save l4p4/64ff4ca470678e185381 to your computer and use it in GitHub Desktop.
ReadFile with JAVA 1.8
This file contains 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
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