Java Text File Write nio writeToFile(File file, String str)

Here you can find the source of writeToFile(File file, String str)

Description

write To File

License

Open Source License

Parameter

Parameter Description
file target file
str string to save

Exception

Parameter Description
IOException an exception

Declaration

public static void writeToFile(File file, String str) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *     Copyright (C) 2017 wysohn/*from   w w  w. j a  v a 2  s . c  o  m*/
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;

import java.nio.channels.FileChannel;

public class Main {
    /**
     *
     * @param file target file
     * @param str string to save
     * @throws IOException
     */
    public static void writeToFile(File file, String str) throws IOException {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        File temp = File.createTempFile("CopyOf_" + file.getName(), ".tmp", file.getParentFile());

        try (FileOutputStream fos = new FileOutputStream(temp);
                OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");) {
            osw.write(str);
        }

        try (FileInputStream istream = new FileInputStream(temp);
                FileOutputStream ostream = new FileOutputStream(file)) {
            FileChannel src = istream.getChannel();
            FileChannel dest = ostream.getChannel();
            dest.transferFrom(src, 0, src.size());
        }

        temp.delete();
    }

    /**
     * same as file.delete() if 'file' is file; recursively deletes all elements inside if 'file' is directory.
     * @param file folder or file
     */
    public static void delete(File file) {
        if (file.isFile()) {
            file.delete();
        } else {
            for (File f : file.listFiles()) {
                delete(f);
            }
            file.delete();
        }
    }
}

Related

  1. writeStringToFile(String s, File f)
  2. writeTextFile(final File file, final String text)
  3. writeTextFile(String file, String articleContent)
  4. writeTo(File file, String content)
  5. writeToFile(File file, String content)
  6. writeToFile(final File file, final String data)
  7. writeToFile(String content, String fileName)
  8. writeToFile(String content, String fileName, boolean createFile)
  9. writeToFile(String filename, String fileContents)