Java File Write via ByteBuffer writeFile(String source, File outputFile)

Here you can find the source of writeFile(String source, File outputFile)

Description

write a string to a file; the file will be overwritten if it already exists

License

Open Source License

Parameter

Parameter Description
source the text to be written
outputFile the file to write the text to

Exception

Parameter Description
IOException an exception

Declaration

public static void writeFile(String source, File outputFile) throws IOException 

Method Source Code


//package com.java2s;
/*//  w  w  w.ja  va  2  s .c  o m
 * Copyright (C) 2010 Alex Cojocaru
 * 
 * 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.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    /**
     * write a string to a file; the file will be overwritten if it already exists
     * @param source the text to be written
     * @param outputFile the file to write the text to
     * @throws IOException
     */
    public static void writeFile(String source, File outputFile) throws IOException {
        writeFile(source, outputFile, true);
    }

    /**
     * write a string to a file
     * @param source the text to be written
     * @param outputFile the file to write the text to
     * @param overwrite true to overwrite the output file, if it exists already
     * @throws IOException
     */
    public static void writeFile(String source, File outputFile, boolean overwrite) throws IOException {

        boolean created = false;

        if (!outputFile.exists()) {
            outputFile.createNewFile();
            created = true;
        } else if (!overwrite) {
            throw new IOException("Destination file already exists");
        }

        byte[] sourceAsBytes = source.getBytes();
        ByteBuffer bb = ByteBuffer.allocate(sourceAsBytes.length);
        bb.put(sourceAsBytes, 0, sourceAsBytes.length);
        bb.flip();

        FileChannel fc = null;
        try {
            fc = new FileOutputStream(outputFile).getChannel();
            fc.write(bb);
        } catch (IOException ioe) {
            // if the operation failed, clean up first before throwing the exception
            if (created)
                outputFile.delete();
            throw ioe;
        } finally {
            if (fc != null)
                fc.close();
        }
    }

    /**
     * @param filepath the path of the file whose existence has to be verified
     * @return true if the file denoted by filepath exists
     */
    public static boolean exists(String filepath) {
        return new File(filepath).exists();
    }
}

Related

  1. writeDouble(BufferedWriter bw, double[] buf)
  2. writeEmpty()
  3. writeFC(String fname, float[] res)
  4. writeFile(File file, byte[] bytes)
  5. writeFile(File file, byte[] data)
  6. writeFileAsByteArray(File file, byte[] par2Data)
  7. writeFileAsStringArray(File file, String[] par2DataArray)
  8. writeFileNIO(String txt, File fyl)
  9. writeFloat(OutputStream os, float val, ByteOrder bo)