Java File Read by Charset doTranseFileCharset(File srcFile, File destFile, String srcCharsetName, String destCharsetName)

Here you can find the source of doTranseFileCharset(File srcFile, File destFile, String srcCharsetName, String destCharsetName)

Description

do Transe File Charset

License

Open Source License

Declaration

private static void doTranseFileCharset(File srcFile, File destFile, String srcCharsetName,
            String destCharsetName) 

Method Source Code


//package com.java2s;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.UnmappableCharacterException;

public class Main {
    private static void doTranseFileCharset(File srcFile, File destFile, String srcCharsetName,
            String destCharsetName) {
        System.out.println("convert file charset. Source file:" + srcFile.getAbsolutePath() + ". Dest file:"
                + destFile.getAbsolutePath());
        String fileContent = getFileContent(srcFile, srcCharsetName);
        if ("".equals(fileContent)) {
            copyFile(destFile, srcFile);
        } else {//from w ww  . j  av a 2 s.  co  m
            saveFile(destFile, fileContent, destCharsetName);
        }
    }

    public static String getFileContent(File file) {
        return getFileContent(file, Charset.defaultCharset().displayName());
    }

    public static String getFileContent(File file, String charsetName) {
        if (file.isFile()) {
            FileInputStream inf = null;
            FileChannel inc = null;
            StringBuilder content = new StringBuilder();
            try {
                inf = new FileInputStream(file);
                inc = inf.getChannel();
                Charset charset = Charset.forName(charsetName);
                CharsetDecoder decoder = charset.newDecoder();
                InputStreamReader reader = new InputStreamReader(inf, decoder);
                char cbuf[] = new char[1024];
                int count = 0;
                while ((count = reader.read(cbuf)) > -1) {
                    content.append(cbuf, 0, count);
                }

                return content.toString();
            } catch (UnmappableCharacterException e) {
                System.err.println("The file's charset is not " + charsetName + ". file:" + file.getAbsolutePath());
                // e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                close(inc);
                close(inf);
            }
        } else {
            // logger.warn("The file is not exists or is not a file!" + file.getAbsolutePath());
        }

        return "";
    }

    public static void copyFile(File destFile, File srcFile) {
        if (!srcFile.exists() || null == destFile) {
            return;
        }
        if (null != destFile.getParentFile()) {
            destFile.getParentFile().mkdirs();
        }
        FileInputStream fis = null;
        FileOutputStream fos = null;
        int buffReaded = 0;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            byte[] buff = new byte[1024];
            while ((buffReaded = fis.read(buff)) != -1) {
                fos.write(buff, 0, buffReaded);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(fis);
            close(fos);
        }
    }

    public static void saveFile(File file, String content, String charsetName) {
        // if (Utils.isEmpty(fileName) || Utils.isEmpty(content)) {
        // return;
        // }
        // logger.info("save file:" + fileName + " charset:" + charsetName);
        file.getParentFile().mkdirs();
        Charset cs;
        if (null == charsetName || "".equals(charsetName)) {
            cs = Charset.defaultCharset();
        } else {
            cs = Charset.forName(charsetName);
        }
        CharsetEncoder encoder = cs.newEncoder();
        FileOutputStream os = null;
        FileChannel out = null;
        try {
            os = new FileOutputStream(file);
            out = os.getChannel();
            out.write(encoder.encode(CharBuffer.wrap(content)));
        } catch (CharacterCodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(out);
            close(os);
        }
    }

    public static void close(Closeable c) {
        if (null != c) {
            try {
                c.close();
                c = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. bomStream(String charset, String file)
  2. copyFileWithEolConversion(File inFile, File outFile, Charset charset)
  3. detectCharset(File f, String[] charsets)
  4. fileContent(File file, Charset charset)
  5. fileToString(final File f, final Charset c)
  6. fileToString(final Path path, final Charset charset)
  7. findPattern(File file, Charset charset, String patternString)