Java Text File Write by Charset writeStringToFile(String fileContent, String filePath, String charset)

Here you can find the source of writeStringToFile(String fileContent, String filePath, String charset)

Description

write String To File

License

Open Source License

Declaration

public static void writeStringToFile(String fileContent, String filePath, String charset)
            throws FileNotFoundException, IOException 

Method Source Code

//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);
    }
}

Related

  1. writerBuffered(final File file, final Charset charset)
  2. writeStreamToString(InputStream is, String charsetName)
  3. writeString(OutputStream os, String string, CharsetEncoder encoder)
  4. writeString(OutputStream out, String charset, String value)
  5. writeStringToFile(File file, String s, String charset)
  6. writeStringToFile(String filename, String contents, Charset encoding)
  7. writeStringToFile(String fileName, String str, String charset)
  8. writeStringToStream(String string, OutputStream stream, Charset charset)
  9. writeTextToFile(String textToWrite, String fileName, Charset cs)