Java Text File Write by Charset write(String toWrite, File file, Charset charset)

Here you can find the source of write(String toWrite, File file, Charset charset)

Description

write

License

Open Source License

Declaration

static boolean write(String toWrite, File file, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w  w. ja v a2  s .  c om
 * This file is part of DeltaEssentials.
 *
 * DeltaEssentials 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.
 *
 * DeltaEssentials 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 DeltaEssentials.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardOpenOption;

public class Main {
    static boolean write(String toWrite, File file) throws IOException {
        return write(toWrite, file, StandardCharsets.UTF_8);
    }

    static boolean write(String toWrite, File file, Charset charset) throws IOException {
        FileLock lock = null;
        FileChannel fileChannel = null;

        try {
            if (!file.exists() && !file.createNewFile()) {
                return false;
            }

            fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
            ByteBuffer buffer = ByteBuffer.wrap(toWrite.getBytes(charset));

            // Lock the file
            lock = fileChannel.lock();

            // Truncate the file (delete the current contents)
            fileChannel.truncate(0);
            fileChannel.write(buffer);

            return true;
        } finally {
            // Release the file lock
            if (lock != null) {
                try {
                    lock.release();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            // Close the file channel
            if (fileChannel != null) {
                try {
                    fileChannel.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            // Note: Do NOT return from a finally-block
        }
    }
}

Related

  1. write(File file, Object content, Charset cs, boolean append)
  2. write(final String s, final OutputStream out, Charset charset)
  3. write(Path file, CharBuffer data, Charset cs)
  4. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  5. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  6. writeAll(Path path, String data, Charset cs)
  7. writeAll(String data, OutputStream outputStream, Charset charset)
  8. writeContent(String content, String charset, File file)
  9. writeFile(File file, String content, Charset charset)