• 使用java进行文件编码转换


    在开发过程中,可能会遇到文件编码的转换,尽管说开发工具eclipse能够转换编码,可是有的情况却非常不方便。比方,原来文件本身的编码是GBK,如今要转换成UTF-8,假设直接在eclipse中把文件编码改动成UTF-8,恭喜你,是乱码,由于不能直接从GBK到UTF-8进行转换,这时就须要我们手动的来转换编码。以下是一个文件编码转换的工具类。

    package com.mikan.stuff;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FilenameFilter;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.nio.charset.Charset;
    import java.nio.charset.UnsupportedCharsetException;
    
    public class FileCharsetConverter {
    
    	public static void main(String[] args) throws Exception {
    		convert("D:\stuff\src\main\java\com\mikan\stuff\test.txt",
    				"GBK", "UTF-8", new FilenameFilter() {
    					@Override
    					public boolean accept(File dir, String name) {
    						return name.endsWith("txt");
    					}
    				});
    	}
    
    	/**
    	 * 把指定文件或文件夹转换成指定的编码
    	 * 
    	 * @param fileName
    	 *            要转换的文件
    	 * @param fromCharsetName
    	 *            源文件的编码
    	 * @param toCharsetName
    	 *            要转换的编码
    	 * @throws Exception
    	 */
    	public static void convert(String fileName, String fromCharsetName,
    			String toCharsetName) throws Exception {
    		convert(new File(fileName), fromCharsetName, toCharsetName, null);
    	}
    
    	/**
    	 * 把指定文件或文件夹转换成指定的编码
    	 * 
    	 * @param file
    	 *            要转换的文件或文件夹
    	 * @param fromCharsetName
    	 *            源文件的编码
    	 * @param toCharsetName
    	 *            要转换的编码
    	 * @throws Exception
    	 */
    	public static void convert(File file, String fromCharsetName,
    			String toCharsetName) throws Exception {
    		convert(file, fromCharsetName, toCharsetName, null);
    	}
    
    	/**
    	 * 把指定文件或文件夹转换成指定的编码
    	 * 
    	 * @param file
    	 *            要转换的文件或文件夹
    	 * @param fromCharsetName
    	 *            源文件的编码
    	 * @param toCharsetName
    	 *            要转换的编码
    	 * @param filter
    	 *            文件名称过滤器
    	 * @throws Exception
    	 */
    	public static void convert(String fileName, String fromCharsetName,
    			String toCharsetName, FilenameFilter filter) throws Exception {
    		convert(new File(fileName), fromCharsetName, toCharsetName, filter);
    	}
    
    	/**
    	 * 把指定文件或文件夹转换成指定的编码
    	 * 
    	 * @param file
    	 *            要转换的文件或文件夹
    	 * @param fromCharsetName
    	 *            源文件的编码
    	 * @param toCharsetName
    	 *            要转换的编码
    	 * @param filter
    	 *            文件名称过滤器
    	 * @throws Exception
    	 */
    	public static void convert(File file, String fromCharsetName,
    			String toCharsetName, FilenameFilter filter) throws Exception {
    		if (file.isDirectory()) {
    			File[] fileList = null;
    			if (filter == null) {
    				fileList = file.listFiles();
    			} else {
    				fileList = file.listFiles(filter);
    			}
    			for (File f : fileList) {
    				convert(f, fromCharsetName, toCharsetName, filter);
    			}
    		} else {
    			if (filter == null
    					|| filter.accept(file.getParentFile(), file.getName())) {
    				String fileContent = getFileContentFromCharset(file,
    						fromCharsetName);
    				saveFile2Charset(file, toCharsetName, fileContent);
    			}
    		}
    	}
    
    	/**
    	 * 以指定编码方式读取文件,返回文件内容
    	 *
    	 * @param file
    	 *            要转换的文件
    	 * @param fromCharsetName
    	 *            源文件的编码
    	 * @return
    	 * @throws Exception
    	 */
    	public static String getFileContentFromCharset(File file,
    			String fromCharsetName) throws Exception {
    		if (!Charset.isSupported(fromCharsetName)) {
    			throw new UnsupportedCharsetException(fromCharsetName);
    		}
    		InputStream inputStream = new FileInputStream(file);
    		InputStreamReader reader = new InputStreamReader(inputStream,
    				fromCharsetName);
    		char[] chs = new char[(int) file.length()];
    		reader.read(chs);
    		String str = new String(chs).trim();
    		reader.close();
    		return str;
    	}
    
    	/**
    	 * 以指定编码方式写文本文件,存在会覆盖
    	 * 
    	 * @param file
    	 *            要写入的文件
    	 * @param toCharsetName
    	 *            要转换的编码
    	 * @param content
    	 *            文件内容
    	 * @throws Exception
    	 */
    	public static void saveFile2Charset(File file, String toCharsetName,
    			String content) throws Exception {
    		if (!Charset.isSupported(toCharsetName)) {
    			throw new UnsupportedCharsetException(toCharsetName);
    		}
    		OutputStream outputStream = new FileOutputStream(file);
    		OutputStreamWriter outWrite = new OutputStreamWriter(outputStream,
    				toCharsetName);
    		outWrite.write(content);
    		outWrite.close();
    	}
    }
    

  • 相关阅读:
    JS-Date日期内置对象
    JS-string内置对象
    MyBatis的事务处理
    MyBatis的简单操作
    MyBatis第一个项目示例
    CSS-盒子模型
    百分比布局的使用
    使用TabLayout快速实现一个导航栏
    彻底理解android中的内部存储与外部存储
    Eclipse的LogCat总是自动清空怎么办?
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4009344.html
Copyright © 2020-2023  润新知