Here you can find the source of writeStringToFile(String fileContent, String filePath, String charset)
public static void writeStringToFile(String fileContent, String filePath, String charset) throws FileNotFoundException, IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void writeStringToFile(String fileContent, String filePath, String charset) throws FileNotFoundException, IOException { File cacheFile = new File(filePath); File fileContents;/* w w w .j a va2s . c om*/ if (cacheFile.exists()) { try { cacheFile.delete(); } catch (Exception var25) { ; } } else { fileContents = cacheFile.getParentFile(); if (!fileContents.exists()) { fileContents.mkdirs(); } } try { cacheFile.createNewFile(); } catch (Exception var24) { ; } cacheFile = new File(filePath); if (cacheFile.exists()) { // fileContentsBytes = null; byte[] fileContentsBytes; if (null != charset && !charset.trim().isEmpty()) { fileContentsBytes = fileContent.getBytes(charset); } else { fileContentsBytes = fileContent.getBytes(); } ByteBuffer buf = ByteBuffer.wrap(fileContentsBytes); FileOutputStream fout = null; FileChannel outc = null; try { fout = new FileOutputStream(filePath); outc = fout.getChannel(); outc.write(buf); } catch (FileNotFoundException var22) { throw var22; } catch (IOException var23) { throw var23; } finally { if (fout != null) { try { fout.close(); } catch (IOException var21) { ; } } if (outc != null) { try { outc.close(); } catch (IOException var20) { ; } } } } } public static void writeStringToFile(String fileContent, String filePath) throws FileNotFoundException, IOException { writeStringToFile(fileContent, filePath, (String) null); } }