Java Text File Write nio writeContent(File file, String content)

Here you can find the source of writeContent(File file, String content)

Description

Writes the string content to the file.

License

Apache License

Parameter

Parameter Description
file a parameter
content a parameter

Declaration

public static void writeContent(File file, String content) 

Method Source Code

//package com.java2s;
/*/*  w  w  w .jav a 2 s.c  o m*/
 * Copyright 2012 James Moger
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

public class Main {
    /**
     * Writes the string content to the file.
     * 
     * @param file
     * @param content
     */
    public static void writeContent(File file, String content) {
        File tempFile = new File(file.getAbsolutePath() + "." + Long.toHexString(System.currentTimeMillis()));
        try {
            file.getAbsoluteFile().getParentFile().mkdirs();
            OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(tempFile),
                    Charset.forName("UTF-8"));
            BufferedWriter writer = new BufferedWriter(os);
            writer.append(content);
            writer.close();
            if (file.exists()) {
                if (!file.delete()) {
                    System.err.println("Failed to delete " + file);
                }
            }
            if (!tempFile.renameTo(file)) {
                System.err.println("Failed to rename temp file to " + file);
            }
        } catch (Throwable t) {
            System.err.println("Failed to write content of " + file);
            t.printStackTrace();
        }
    }

    /**
     * Writes the string content to the file.
     * 
     * @param file
     * @param content
     */
    public static void writeContent(File file, byte[] data) {
        File tempFile = new File(file.getAbsolutePath() + "." + Long.toHexString(System.currentTimeMillis()));
        try {
            file.getAbsoluteFile().getParentFile().mkdirs();
            FileOutputStream os = new FileOutputStream(tempFile);
            os.write(data);
            os.close();
            if (file.exists()) {
                file.delete();
            }
            tempFile.renameTo(file);
        } catch (IOException e) {
            throw new RuntimeException("Error writing to file " + file, e);
        }
    }

    /**
     * Delete a file or recursively delete a folder.
     * 
     * @param fileOrFolder
     * @return true, if successful
     */
    public static boolean delete(File fileOrFolder) {
        boolean success = false;
        if (fileOrFolder.isDirectory()) {
            File[] files = fileOrFolder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        success |= delete(file);
                    } else {
                        success |= file.delete();
                    }
                }
            }
        }
        success |= fileOrFolder.delete();
        return success;
    }
}

Related

  1. write(String msg)
  2. writeAllLines(File file, List list)
  3. writeAllText(File file, String contents)
  4. writeArray2File(File file, Object[] objects, String code)
  5. writeAscii(CharSequence ascii, OutputStream os)
  6. writeContent(File file, String content)
  7. writeData(String address, BigInteger toWrite)
  8. writeDataFromList(String address, ArrayList toWrite)
  9. writeFileContent(File file, String content)