Java Write String to File writeFile(String filename, String data)

Here you can find the source of writeFile(String filename, String data)

Description

write File

License

GNU General Public License

Declaration

public static void writeFile(String filename, String data) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w ww.  j  a  va2s.co m*/
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on Nov 4, 2016
 * Author: blivens 
 *
 */

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

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

public class Main {
    public static void writeFile(String filename, String data) throws IOException {
        if (filename == null || filename.isEmpty() || filename.equals("-")) {
            System.out.println(data);
        } else {
            filename = expandUserHome(filename);
            File file = new File(filename);
            try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
                writer.println(data);
            }
        }
    }

    public static String expandUserHome(String file) {
        if (file.startsWith("~" + File.separator)) {
            file = System.getProperty("user.home") + file.substring(1);
        }
        return file;
    }
}

Related

  1. writeFile(String fileName, String content, String encoding)
  2. writeFile(String fileName, String contents)
  3. writeFile(String fileName, String contents)
  4. writeFile(String filename, String contents)
  5. writeFile(String fileName, String contentStr, String charset)
  6. writeFile(String FileName, String data)
  7. writeFile(String fileName, String data)
  8. writeFile(String fileName, String encoding, String content)
  9. writeFile(String fileName, String fileContent)