Here you can find the source of writeContent(String content, String charset, File file)
public static void writeContent(String content, String charset, File file) throws IOException
//package com.java2s; /**/*from ww w. j a v a2 s . c om*/ * Copyright 2013-2018 the original author or authors from the Jeddict project (https://jeddict.github.io/). * * 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.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; public class Main { public static void writeContent(String content, File file) throws IOException { writeContent(content, Charset.defaultCharset().toString(), file); } public static void writeContent(String content, String charset, File file) throws IOException { if (file.isDirectory()) { throw new IOException("Cannot write content to directory"); } if (file.exists()) { file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); fos.write(content.getBytes(charset)); fos.close(); } public static byte[] getBytes(File file) throws IOException { if (!file.exists()) { throw new FileNotFoundException(); } if (file.length() > Integer.MAX_VALUE) { throw new IOException("file is too large for single read"); } byte[] bytes = new byte[(int) file.length()]; try (FileInputStream fileInputStream = new FileInputStream(file)) { int read = fileInputStream.read(bytes); if (read != file.length()) { throw new IOException("could not read entire file"); } } return bytes; } }