67,549
社区成员




File file = new File("D:/workspace_a/a/src/a/a.txt");
File fileb = new File("D:/workspace_a/a/src/a/b.txt");
String line= "";
BufferedReader reader =null;
OutputStreamWriter out = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileb)));
out = new OutputStreamWriter(new FileOutputStream(file));
while ((line=reader.readLine())!=null) {
out.write(line);//utf8
String gbkline=utf8ToGbk(line);//这里应该怎么转
out.write("\n");
out.write(gbkline);//gbk
}
} catch (IOException e1) {
e1.printStackTrace();
} finally{
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String utf8Togb2312(String str){
StringBuffer sb = new StringBuffer();
for(int i=0; i<str.length(); i++) {
char c = str.charAt(i);
switch (c) {
case '+':
sb.append(' ');
break;
case '%':
try {
sb.append((char)Integer.parseInt(str.substring(i+1,i+3),16));
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 2;
break;
default:
sb.append(c);
break;
}
}
String result = sb.toString();
String res=null;
try {
byte[] inputBytes = result.getBytes("8859_1");
res= new String(inputBytes,"UTF-8");
}
catch(Exception e){}
return res;
}
public static String utf82gbk(String utf) {
String l_temp = utf8ToUnicode(utf);
l_temp = Unicode2GBK(l_temp);
return l_temp;
}
/**
* utf-8 转unicode
*
* @param inStr
* @return String
*/
public static String utf8ToUnicode(String inStr) {
char[] myBuffer = inStr.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < inStr.length(); i++) {
UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]);
if (ub == UnicodeBlock.BASIC_LATIN) {
sb.append(myBuffer[i]);
} else if (ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
int j = (int) myBuffer[i] - 65248;
sb.append((char) j);
} else {
short s = (short) myBuffer[i];
String hexS = Integer.toHexString(s);
String unicode = "\\u" + hexS;
sb.append(unicode.toLowerCase());
}
}
return sb.toString();
}
/**
*
* @param dataStr
* @return String
*/
public static String Unicode2GBK(String dataStr) {
int index = 0;
StringBuffer buffer = new StringBuffer();
int li_len = dataStr.length();
while (index < li_len) {
if (index >= li_len - 1
|| !"\\u".equals(dataStr.substring(index, index + 2))) {
buffer.append(dataStr.charAt(index));
index++;
continue;
}
String charStr = "";
charStr = dataStr.substring(index + 2, index + 6);
char letter = (char) Integer.parseInt(charStr, 16);
buffer.append(letter);
index += 6;
}
return buffer.toString();
}
private void changeEncoding(File sourceFile,File targetFile) throws IOException {
FileInputStream fin = null;
FileOutputStream fout= null;
FileChannel fcin = null;
FileChannel fcout = null;
if (targetEncoding == null) {
this.targetEncoding = System.getProperty("file.encoding");
}
try {
fin = new FileInputStream(sourceFile);
fout = new FileOutputStream(targetFile);
fcin = fin.getChannel();
fcout = fout.getChannel();
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while(true){
buffer.clear();
int r = fcin.read(buffer);
if (r == -1) break;
buffer.flip();
byte[] bys = Charset.forName(sourceEncoding).decode(buffer)
.toString().getBytes(targetEncoding);
fcout.write(buffer.wrap(bys));
}
} catch (Exception e) {
} finally {
if (fin != null) {
fin.close();
}
if (fcin != null) {
fcin.close();
}
if (fout != null) {
fout.close();
}
if (fcout != null) {
fcout.close();
}
}
}
String utf8 = new String(t.getBytes( "UTF-8"));
System.out.println(utf8);
String unicode = new String(utf8.getBytes(),"UTF-8");
System.out.println(unicode);
String gbk = new String(unicode.getBytes("GBK"));
System.out.println(gbk);
这样呢?