import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class changecode{
public static void main(String []args)throws Exception
{
//paiOne("test2.txt","UTF-8","GB2312");
paiOne(args[0],args[1],args[2]);
}
// Verify that no character has a hex value greater than 0xFFFD, or less than 0x20.
// Check that the character is not equal to the tab ("t), the newline ("n), the carriage return ("r), or is an invalid XML character below the range of 0x20. If any of these characters occur, an exception is thrown.
public static String CheckUnicodeString(String values)
{
char[] value=new char[values.length()];value=values.toCharArray();
for (int i=0; i < values.length(); ++i) {
if (value[i] > 0xFFFD)
{
value[i]='\n';
// throw new Exception("Invalid Unicode");//或者直接替换掉0x0 value[i]='"n';
}
else if (value[i] < 0x20 && value[i] != '\t' & value[i] != '\n' & value[i] != '\r')
{
value[i]='\n';
// throw new Exception("Invalid Xml Characters");//或者直接替换掉0x0 value[i]='"n';
}
}
String s=new String(value);
return s;
}
private static void paiOne(String files,String sourceCode,String targetCode)
throws Exception
{
File file=new File(files);
String strOut;
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), sourceCode));
String line = "";
String[] temp = (String[])null;
BufferedWriter bw = null;
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( "result.txt", false), targetCode));
while ((line = br.readLine()) != null)
{
byte[] b = line.getBytes(sourceCode);
String str=new String(b,sourceCode);
byte[] c=str.getBytes(targetCode);
strOut = new String(c, targetCode);
if(targetCode.equals("utf-8")||targetCode=="UTF-8" ||targetCode=="unicode")
strOut=CheckUnicodeString(strOut);
bw.write(strOut);
bw.newLine();
}
br.close();
br = null;
line = null;
bw.flush();
bw.close();
bw = null;
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class changecode{
public static void main(String []args)throws Exception
{
//paiOne("test2.txt","UTF-8","GB2312");
paiOne(args[0],args[1],args[2]);
}
// Verify that no character has a hex value greater than 0xFFFD, or less than 0x20.
// Check that the character is not equal to the tab ("t), the newline ("n), the carriage return ("r), or is an invalid XML character below the range of 0x20. If any of these characters occur, an exception is thrown.
public static String CheckUnicodeString(String values)
{
char[] value=new char[values.length()];value=values.toCharArray();
for (int i=0; i < values.length(); ++i) {
if (value[i] > 0xFFFD)
{
value[i]='\n';
// throw new Exception("Invalid Unicode");//或者直接替换掉0x0 value[i]='"n';
}
else if (value[i] < 0x20 && value[i] != '\t' & value[i] != '\n' & value[i] != '\r')
{
value[i]='\n';
// throw new Exception("Invalid Xml Characters");//或者直接替换掉0x0 value[i]='"n';
}
}
String s=new String(value);
return s;
}
private static void paiOne(String files,String sourceCode,String targetCode)
throws Exception
{
File file=new File(files);
String strOut;
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), sourceCode));
String line = "";
String[] temp = (String[])null;
BufferedWriter bw = null;
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( "result.txt", false), targetCode));
while ((line = br.readLine()) != null)
{
byte[] b = line.getBytes(sourceCode);
String str=new String(b,sourceCode);
byte[] c=str.getBytes(targetCode);
strOut = new String(c, targetCode);
if(targetCode.equals("utf-8")||targetCode=="UTF-8" ||targetCode=="unicode")
strOut=CheckUnicodeString(strOut);
bw.write(strOut);
bw.newLine();
}
br.close();
br = null;
line = null;
bw.flush();
bw.close();
bw = null;
}
}