//*****************************************************************************
// Write to file using Java "nio" package                           
// Write entire file from a List                         
//*****************************************************************************
import java.nio.file.*;                             //import nio.file package
import java.nio.charset.*;                          //used for Charset (if used)
import java.util.*;                                 //used for the List class

public class WriteFromList   
{
    public static void main(String[] args)
    {
        try {
            Path outfile = Paths.get("/home/s/sultans/web/java/demo/8inpout/data/temp2.txt");

            String[] array = { "this is line 1",
                               "this is line 2",
                               "this is line 3" };
 
            List<String> lines = new ArrayList<String>();       //create an ArrayList as a List

            for (String line : array)                           //iterate thru the array
                lines.add(line);                                //copy each line into the list
 
 
            Files.write(outfile, lines);
//          Files.write(outfile, lines, Charset.forName("UTF-16"));      
//          Files.write(outfile, lines, StandardOpenOption.TRUNCATE_EXISTING);   //or .WRITE   
                                                                                 //or .APPEND
            System.out.println("Write Successful");
        }
        catch(Exception e) {
            System.out.print(e);                                //print the exception
            e.printStackTrace();                                //print trace dump
        }
    }
}