Java Text File Write nio writeAllLines(File file, List list)

Here you can find the source of writeAllLines(File file, List list)

Description

write All Lines

License

Open Source License

Declaration

public static void writeAllLines(File file, List<String> list) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2013, 2014 ETAS GmbH.//  w w  w  .  j  a  v  a 2s  . c o m
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Dennis Eder (ETAS GmbH) - initial API and implementation and/or initial documentation
*******************************************************************************/

import java.io.BufferedWriter;
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.util.List;

public class Main {
    final static Charset ENCODING = StandardCharsets.UTF_8;

    public static void writeAllLines(File file, List<String> list) throws IOException {
        writeAllLines(file, list.toArray(new String[] {}));

    }

    public static void writeAllLines(File file, String[] lines) throws IOException {
        Path path = file.toPath();
        try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)) {
            for (String line : lines) {
                writer.write(line);
                writer.newLine();
            }
        }

    }
}

Related

  1. write(final StringBuffer data, final Writer output)
  2. write(OutputStream stream, String string)
  3. write(String content, String file)
  4. write(String location, String content)
  5. write(String msg)
  6. writeAllText(File file, String contents)
  7. writeArray2File(File file, Object[] objects, String code)
  8. writeAscii(CharSequence ascii, OutputStream os)
  9. writeContent(File file, String content)