`

文件字符编码转换工具

    博客分类:
  • Life
UP 
阅读更多
public class CharsetConvertor {
	public final static String PATH = "";
	public final static String[] FILTER_WORD = new String[] {  };

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		cpdetector.io.CodepageDetectorProxy detector = cpdetector.io.CodepageDetectorProxy
				.getInstance();
		detector.add(cpdetector.io.JChardetFacade.getInstance());
		java.nio.charset.Charset charset = null;
		File dir = new File(PATH);
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			for (File file : files) {
				if (!file.isDirectory()) {
					try {
						charset = detector.detectCodepage(file.toURL());
					} catch (Exception ex) {
						System.err.println("获取文件[" + file.getAbsolutePath() + "]编码失败!");
						ex.printStackTrace();
					}

					if (charset == null || !"GB2312".equals(charset.name())) {
						// 对于非gbk的转换为gbk
						String path = file.getParent() + "\\gbk\\";
						File gbkDir = new File(path);
						if (!gbkDir.exists()) {
							if (!gbkDir.mkdir()) {
								throw new RuntimeException("创建gbk文件夹[" + path + "]失败");
							}
						}
						String name = file.getName();
						for (String word : FILTER_WORD) {
							name = name.replaceAll(word, "");
						}
						String fileName = path + name;
						System.out.println(fileName);

						try {
							convert(file.getAbsolutePath(), fileName, "UTF-8", "GB2312");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}

				}
			}
		} else {
			throw new RuntimeException("指定的路径[" + dir.getAbsolutePath() + "]不正确.");
		}

	}

	/**
	 * 
	 * @param infile
	 *            源文件路径
	 * @param outfile
	 *            输出文件路径
	 * @param fromCharset
	 *            源文件编码
	 * @param toCharset
	 *            目标文件编码
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public static void convert(String infile, String outfile, String fromCharset, String toCharset)
			throws IOException, UnsupportedEncodingException {
		// set up byte streams
		InputStream in;
		if (infile != null)
			in = new FileInputStream(infile);
		else
			in = System.in;
		OutputStream out;
		if (outfile != null)
			out = new FileOutputStream(outfile);
		else
			out = System.out;

		// Use default encoding if no encoding is specified.
		if (fromCharset == null)
			fromCharset = System.getProperty("file.encoding");
		if (toCharset == null)
			toCharset = System.getProperty("file.encoding");

		// Set up character stream
		Reader r = new BufferedReader(new InputStreamReader(in, fromCharset));
		Writer w = new BufferedWriter(new OutputStreamWriter(out, toCharset));

		// Copy characters from input to output. The InputStreamReader
		// converts from the input encoding to Unicode,, and the
		// OutputStreamWriter
		// converts from Unicode to the output encoding. Characters that cannot
		// be
		// represented in the output encoding are output as '?'
		char[] buffer = new char[4096];
		int len;
		while ((len = r.read(buffer)) != -1)
			w.write(buffer, 0, len);
		r.close();
		w.flush();
		w.close();
	}

}
分享到:
评论

相关推荐

    批量文件字符编码转换工具

    相当不错的文件编码转换工具,批量转换,省时省力。极力推荐!!

    批量字符编码转换工具 20070709

    "批量字符编码转换工具"是绿色软件,单文件免安装,请放心试用。 批量字符编码转换工具可以在gbk,gb23123,unicode,utf8,ansi之间进行汉字编码转换。 批量字符编码转换工具的特色: 1 支持格式众多的字符串编码转换器 ...

    多功能文件字符集编码转换工具

    标题中的“多功能文件字符集编码转换工具”是指一个软件或程序,它的主要功能是帮助用户在不同的字符编码之间进行转换。在计算机科学中,字符集编码是用于将字符(如字母、数字和符号)转化为二进制表示的方式,常见...

    全能字符编码转换工具

    使用《全能字符编码转换工具》,用户可以轻松地将文本文件从一种编码转换为另一种,这对于处理不同来源的文本数据,尤其是涉及中文字符的文件时,是非常实用的。例如,如果你收到一份GBK编码的文档,但你的系统默认...

    批量字符编码转换工具,批量转换文件编码 超级批量编码转换

    批量转换文件编码 超级批量编码转换 批量转换编码 编码批量转换工具 批量转换txt编码 linux 批量转换编码 编码格式批量转换 php 字符编码转换 字符编码转换 java 字符编码转换

    工具-字符编码转换

    本文将基于“工具-字符编码转换”的主题,深入探讨字符编码的概念、常见编码格式、字符编码转换工具以及如何进行实际操作。 字符编码是计算机表示和处理文字的一种方式,它将各种字符与二进制数字对应起来。常见的...

    字符编码过滤器 字符编码转换 post字符转换

    本文将深入探讨"字符编码过滤器"、"J2EE字符编码"、"字符编码转换"以及"POST字符转换"这些关键知识点,并结合提供的文件"encoding-filter.jar"和"使用方法.txt"来解释它们的应用。 首先,字符编码是计算机存储和...

    字符编码转换处理工具

    字符编码转换工具可以帮助我们将字符转换成这两种形式,便于查看和理解。 再来说说ANSI码,它通常指的是Windows系统中的“Windows-1252”编码,一种扩展的ASCII编码,包含了更多的西欧语言字符。在某些情况下,如果...

    CharTools 字符编码转换工具(1.0.1.0)

    《CharTools字符编码转换工具详解》 在信息技术领域,字符编码是至关重要的组成部分,它决定了计算机如何理解和显示各种字符。CharTools(版本1.0.1.0)是一款专为字符编码转换设计的实用工具,提供了多种编码形式...

    批量文件编码转换工具

    本工具名为"批量文件编码转换工具",专门针对那些需要处理大量文件编码问题的用户,能够有效地节省时间和精力。 GB2312编码,全称“中华人民共和国国家标准汉字编码”,是简体中文字符集的一种标准,主要应用于早期...

    编码批量转换工具

    "批量字符编码转换工具"是绿色软件,单文件免安装,请放心试用。 批量字符编码转换工具可以在gbk,gb23123,unicode,utf8,ansi之间进行汉字编码转换。 【软件特点】 1 支持格式众多的字符串编码转换器 支持几十种字符...

    文件任意编码转换工具 ExecuteConvertFile

    《文件任意编码转换工具ExecuteConvertFile详解》 在信息技术领域,数据编码是至关重要的环节,尤其是在处理文本文件时。不同的编码格式可能导致字符显示异常,甚至出现乱码问题。为了解决这一难题,开发者们设计了...

    万能的文件编码转换工具。

    本软件“万能的文件编码转换工具”专为此问题提供解决方案,它支持多种编码格式,使得用户能够轻松应对各种编码不兼容的情况。 首先,我们需要理解什么是文件编码。文件编码是指将字符转化为二进制数据的规则,常见...

    文件编码转换工具

    在处理跨平台或跨语言的文件时,文件编码的不一致可能导致乱码问题,这时就需要文件编码转换工具来解决。 "文件编码转换工具"是一种专门用于处理不同编码格式之间转换的软件。这种工具通常支持常见的编码格式,如...

    文件字符编码转换

    "文件字符编码转换"的主题着重于如何将不同编码格式的文件或文件夹在GBK、GB2312、Unicode(USC-2)以及UTF-8之间进行转换。 1. **GBK与GB2312**:这两个都是中国国家标准的简体中文字符编码。GB2312是早期的编码...

    字符编码转换器(Encoding Tool)EncodingTool

    "Encoding Tool"正是这样一款专为解决字符编码转换而设计的实用工具,它简化了这一过程,使得用户可以方便地在各种编码之间进行切换。 首先,让我们了解什么是字符编码。字符编码是将字符与数字进行对应的一种规则...

    字符串转换工具

    1. **字符编码转换**:服务器可能会接收到不同编码格式的数据,如UTF-8、GBK等。字符串转换工具可以帮助我们将这些数据统一转换为服务器所支持的编码,避免乱码问题。 2. **URL编码解码**:在HTTP协议中,URL中的...

    java 写的字符编码转换工具(附带源码)

    通过研究这个字符编码转换工具,不仅可以提升对Java字符编码的理解,还可以学习到文件操作、错误处理、用户交互等方面的知识,对于提升Java编程技能大有裨益。同时,源码分析也有助于我们学习和借鉴优秀的编程实践,...

    字符集编码转换工具

    在信息化社会,数据交流频繁,各种文本文件、网页等都需要在不同的系统和平台间传递,而这些系统可能采用不同的字符编码标准,这就需要有字符集编码转换工具来确保信息的正确性和完整性。 字符集编码是计算机存储和...

Global site tag (gtag.js) - Google Analytics