文件(一)--编码问题
来源:互联网 发布:淘宝如何改会员名 编辑:程序博客网 时间:2024/06/05 19:30
几种常见编码如下:
import java.io.UnsupportedEncodingException;public class CoreEncoder {public static void main(String[] args) throws Exception {String str = "就这样YWD";byte[] byte1 = str.getBytes();System.out.print("默认GBK :");for(byte b:byte1){//把字节转换成int以16进制方式表现出来;System.out.print(Integer.toHexString(b&0xff)+" ");}System.out.println();byte[] byte2 = str.getBytes("gbk");System.out.print("GBK :");for(byte b:byte2){//设置编码为gbk输出System.out.print(Integer.toHexString(b&0xff)+" ");}System.out.println();byte[] byte3 = str.getBytes("utf-8");System.out.print("utf-8 :");for(byte b:byte3){//设置编码为utf-8输出System.out.print(Integer.toHexString(b&0xff)+" ");}System.out.println();byte[] byte4 = str.getBytes("utf-16be");System.out.print("utf-16be:");for(byte b:byte4){//设置编码为gbk输出System.out.print(Integer.toHexString(b&0xff)+" ");}System.out.println();String str1 = new String(byte4);//用项目默认编码System.out.println(str1);String str2 = new String(byte4,"utf-16be");//用对应编码编码System.out.println(str2);}/* * 总结:gbk编码中文两个字节,英文一个; * utf-8编码中文三个字节,英文一个; * utf-16be中英文都是两个字节; * 当你字节序列是某种编码时,想要将其变成字符串也必须用这种编码; */}看以上注释,不用多解释了。
文件内容编码转换
public class IOCVUtils { /** 源文件编码 */ public static String sourceEncoding = "GBK"; /** 目标编码 */ public static String targetEncoding = "UTF-8"; /** * 文件内容转编码 * @param sourceFile * @param targetFile * @throws UnsupportedEncodingException * @throws FileNotFoundException * @throws IOException */ public static void changeEncoding(File sourceFile, File targetFile) throws UnsupportedEncodingException, FileNotFoundException, IOException { FileInputStream fin = null; FileOutputStream fout = null; FileChannel fcin = null; FileChannel fcout = null; if (sourceEncoding == null) { IOCVUtils.sourceEncoding = 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(); fcout.write(ByteBuffer.wrap(Charset.forName(sourceEncoding).decode(buffer).toString().getBytes(targetEncoding))); } } finally { if (fin != null) { fin.close(); fin = null; } if (fcin != null) { fcin.close(); fcin = null; } if (fout != null) { fout.close(); fout = null; } if (fcout != null) { fcout.close(); fcout = null; } } } /** * 文件内容转编码 * @param sourceFile * @param targetFile * @throws UnsupportedEncodingException * @throws FileNotFoundException * @throws IOException */ public static void changeEncoding(String sourceFile, String targetFile) throws UnsupportedEncodingException, FileNotFoundException, IOException{ File fl1 = new File(sourceFile); File fo1 = new File(targetFile); changeEncoding(fl1, fo1); } /** * 文件内容转编码 * @param sourceFile * @param targetFile * @param sourceEncoding 源文件编码 默认源文件的系统存储编码 System.getProperty("file.encoding"); * @param targetEncoding 目标编码 默认utf-8 * @throws UnsupportedEncodingException * @throws FileNotFoundException * @throws IOException */ public static void changeEncoding(String sourceFile, String targetFile, String sourceEncoding, String targetEncoding) throws UnsupportedEncodingException, FileNotFoundException, IOException { IOCVUtils.sourceEncoding = sourceEncoding; IOCVUtils.targetEncoding = targetEncoding; changeEncoding(sourceFile, targetFile); } }
0 0
- 文件(一)--编码问题
- 编码问题学习(一)
- python中使用文件的读取编码问题和简单正则使用(一)
- 关于编码(一):url的编码和解码问题
- 爬虫学习(一):编码问题
- 解决python编码问题(一)
- Java Web中的编码问题(一)
- python学习一(编码问题)
- python2.7 编码问题解析(一)
- 一、Python编码问题
- 文件编码问题集锦
- 文件的编码问题
- .NET 文件编码问题
- 文件编码问题集锦
- eclipse 文件编码问题
- egrep 文件编码问题
- 文件编码问题
- LInux 文件 编码问题
- ScrollView嵌套ListView只显示一行
- 表单中input 属性name和id有什么区别 用途是什么
- servlet入门
- sicily1046
- 【iOS开发-4】UIApplication生命周期以及如何简单测试每个函数何时被调用?
- 文件(一)--编码问题
- java学习笔记18
- GIT和SVN之间的五个基本区别
- ODI和OWB
- Qt中使用QSqlDatabase::removeDatabase()的正确方法
- 第6章 数据库管理之事务
- 学习
- Extjs4.2边学边用之panel
- Linux之latex的初学教程