Here you can find the source of write(String toWrite, File file, Charset charset)
static boolean write(String toWrite, File file, Charset charset) throws IOException
//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 } } }