BufferedReader reader = null; try { reader = new BufferedReader( new InputStreamReader( new FileInputStream(FILE_NAME), Charset.forName("UTF-8"))); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { // log error } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // log warning } } }
InputStreamReader in = null; try { in = new InputStreamReader(new FileInputStream(new File(FILE_NAME)), Charsets.UTF_8); LineIterator it = new LineIterator(in); while (it.hasNext()) { System.out.println(it.nextLine()); } } catch (IOException e) { // log error } finally { IOUtils.closeQuietly(in); }
LineIterator it = null; try { it = FileUtils.lineIterator(new File(FILE_NAME), "UTF-8"); while (it.hasNext()) { System.out.println(it.nextLine()); } } catch (IOException e) { // log error } finally { if (it != null) { it.close(); } }
try (BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream(FILE_NAME), StandardCharsets.UTF_8))){ String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { // log error }
List<String> lines = Files.readAllLines(Paths.get(FILE_NAME), StandardCharsets.UTF_8); for(String line: lines){ System.out.println(line); }
Files.lines(Paths.get(FILE_NAME), StandardCharsets.UTF_8).forEach(System.out::println);
Source: https://habr.com/ru/post/269667/
All Articles