Write File Java Using Read Lines

Write File Java Using Read Lines

In this example we will use Java 7 package java.nio.file.Files new feature which readAllLines of input file and return back the List object which contains data and to write data to another file we will sue Files.write method operation as below:

Java Class:

package com.javahonk;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class WriteFileUsingFileWrite {
	
	private final static String WRITE_TO_FILE_NAME = "C:\\JavaHonk\\File\\WriteToFile.txt";
	private final static String READ_TO_FILE_NAME = "C:\\JavaHonk\\File\\JavaHonk.txt";
	private final static Charset ENCODING = StandardCharsets.UTF_8;

	public static void main(String args[]) throws IOException {

		File writeToFile = new File(WRITE_TO_FILE_NAME);
		Path writeFilePath = Paths.get(WRITE_TO_FILE_NAME);
		Path readFilePath = Paths.get(READ_TO_FILE_NAME);
		
		if (!writeToFile.exists()) {
			writeToFile.createNewFile();
		}
		
		List<String> lines = Files.readAllLines(readFilePath, ENCODING);
		Files.write(writeFilePath, lines, ENCODING);		
		
	}
	

}

Output:

Write File Java Using Read Lines

For more information about this package please read oracle documentation here 

Leave a Reply

Your email address will not be published. Required fields are marked *