教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java实现在不同编码之间进行文件转换程序

java实现在不同编码之间进行文件转换程序

发布时间:2016-10-25   编辑:jiaochengji.com
教程集为您提供java实现在不同编码之间进行文件转换程序等资源,欢迎您收藏本站,我们将为您提供最新的java实现在不同编码之间进行文件转换程序资源
本文章给大家介绍的是一个java实现在不同编码之间进行文件转换程序例子,希望程序对各位有帮助。

java实现在不同编码之间进行文件转换,使用 InputStreamReader 或者FileReader 类,它们可以自动地把某个特定字符编码转换为本地字符代码。否则就使用DataOutputStream 类当中的writeUTF()方法以Unicode 文本写字符串,当然,读取的时候必须使用DataInputStream 打开它,并且使用readUTF()方法读取这些字符串。
为什么要转换编码呢?大家都知道,Java 语言是以Unicode 为基础的,但是操作系统都有它们自己内部的可能是与Unicode 不兼容的编码方式,所以用户收到的输入可能属于不同的代码系统,程序显示给用户的字符串最终必须使用当地的操作系统可以识别的方法对其进行译码。
转换不同编码,具体实现步骤:

1.编写ConvertEncoding 类的基本框架,该类包括main()方法、usage()方法和convert()方法:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8241')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8241>public class ConvertEncoding
{
public static void main(String[] args);
public static void usage();
public static void convert(String infile, String outfile, String from, String to)
throws IOException, UnsupportedEncodingException;
}

2.Main()方法实现了实现了把一种编码形式的文件,转换成为另外一种编码形式:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6629')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6629>

public static void main(String[] args)
{
String from = null, to = null;
String infile = null, outfile = null;
for(int i = 0; i <args.length; i ) { // 处理命令行参数
if (i == args.length-1) usage(); .
if (args[i].equals("-from")) from = args[ i];
else if (args[i].equals("-to")) to = args[ i];
else if (args[i].equals("-in")) infile = args[ i];
else if (args[i].equals("-out")) outfile = args[ i];
else usage();
}
try { convert(infile, outfile, from, to); } // 开始转换
catch (Exception e)
{ // 处理例外
System.exit(1);
}
}

3.usage()方法实现了提醒用户命令行的正确输入,代码如下:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1781')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1781>

public static void usage()
{
System.err.println("Usage: java ConvertEncoding <options>\n"
"Options:\n\t-from <encoding>\n\t"
"-to <encoding>\n\t"
"-in <file>\n\t-out <file>");
System.exit(1);
}

4.Convert()方法实现了编码方式的转换,代码如下:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7948')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7948>public static void convert(String infile, String outfile, String from, String to)
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;
// 设置缺省的编码方式
if (from == null) from = System.getProperty("file.encoding");
if (to == null) to = System.getProperty("file.encoding");
// 建立用于读写的字符流
Reader r = new BufferedReader(new InputStreamReader(in, from));
Writer w = new BufferedWriter(new OutputStreamWriter(out, to));
/** 把字符从输入Copy 到输出。InputStreamReader
* 把字符从原始编码方式转为Unicode
* OutputStreamWriter 则把字符从Unicode 编码转换为指定的输出编码方式
* 不能用指定输出编码方式编码的字符输出为'?'
**/
char[] buffer = new char[4096];
int len;
while((len = r.read(buffer)) != -1) .
w.write(buffer, 0, len); .
r.close(); .
w.close();
}

注意:ConvertEncoding 类需要引入import java.io.*;

您可能感兴趣的文章:
java实现在不同编码之间进行文件转换程序
ASP与JSP的比较(一)
php怎么传给java
go 协程
Python是一门怎样的编程语言
opcode cache与JIT之间有哪些区别
Python的特点(优点和缺点)
Java开发环境的过去、现在和将来
JDBC接口技术 <转>
js 文件编码转换教程

[关闭]
~ ~