Java – Reading a File at once

\Z is the EOF (End of File) Symbol. When set as delimiter the Scanner will read the fill until the EOF Flag is reached.

 

package com.pwn.nio;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadingFileAtOnce {

	public static void main(String[] args) {
		File f = new File("c://install//instructions.txt");
		try (Scanner scanner = new Scanner(f)) {
			String content = scanner.useDelimiter("\\Z").next();
			System.out.println(content);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}

 
This is sample Instructions File


 

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s