Java – read files using try-with-resources

The try-with-resources statement is a try statement that declares one or more resources.   A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.
Any object that implements java.lang.AutoCloseable, which includes all objects which implement, can be used as a resource.

package com.pwn.nio;

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

public class NIOReadFile {

	public static void main(String[] args) {
		String file = "c://install//instructions.txt";
		try (Stream stream = Files.lines(Paths.get(file))) {


		} catch (IOException e) {



This is sample Instructions File

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s