Java Text File Save writeFile(List lines, String filePath)

Here you can find the source of writeFile(List lines, String filePath)

Description

Write a list of lines to a file.

License

Open Source License

Parameter

Parameter Description
lines the list of lines to write.
outFile the path to the file to write to.

Exception

Parameter Description
IOException an exception

Declaration

public static void writeFile(List<String> lines, String filePath) throws IOException 

Method Source Code

//package com.java2s;
/*// w w  w  . jav  a  2  s.c o  m
 * Stage - Spatial Toolbox And Geoscript Environment 
 * (C) HydroloGIS - www.hydrologis.com 
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * (http://www.eclipse.org/legal/epl-v10.html).
 */

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

import java.util.List;

public class Main {
    /**
     * Write text to a file in one line.
     * 
     * @param text the text to write.
     * @param file the file to write to.
     * @throws IOException 
     */
    public static void writeFile(String text, File file) throws IOException {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(text);
        } finally {
            if (bw != null)
                bw.close();
        }
    }

    /**
     * Write a list of lines to a file.
     * 
     * @param lines the list of lines to write.
     * @param outFile the path to the file to write to.
     * @throws IOException 
     */
    public static void writeFile(List<String> lines, String filePath) throws IOException {
        writeFile(lines, new File(filePath));
    }

    /**
     * Write a list of lines to a file.
     * 
     * @param lines the list of lines to write.
     * @param file the file to write to.
     * @throws IOException 
     */
    public static void writeFile(List<String> lines, File file) throws IOException {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            for (String line : lines) {
                bw.write(line);
                bw.write("\n"); //$NON-NLS-1$
            }
        } finally {
            if (bw != null)
                bw.close();
        }
    }
}

Related

  1. writeFile(final String outputDir, final String response, final String fileName)
  2. writeFile(IFile annotationFile, StringBuffer head, String annotatedSignature, String nextLines, BufferedReader tailReader, IProgressMonitor monitor)
  3. writeFile(IFile file, final InputStream contents, final String charset, IProgressMonitor monitor)
  4. writeFile(List data, File f)
  5. writeFile(List fileLines, String pathname)
  6. writeFile(List strList, String fileFullPath)
  7. writeFile(List txtList, String MyFilePath)
  8. writeFile(String file, String content)
  9. writeFile(String file, String text)