UTF-8转换成GBK
来源:互联网 发布:php用户在线留言 编辑:程序博客网 时间:2024/06/09 10:30
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;public class FindAllTxtFile {private static String content;public static void main(String[] args) throws Exception {// 递归显示C盘下所有文件夹及其中文件File root = new File("c:/app");showAllFiles(root);}public static String PrintOut(String context, String encodeFormat) {if (encodeFormat == null || encodeFormat.equals(""))encodeFormat = "UTF-8";try {byte[] content = context.getBytes();String result = new String(content, encodeFormat);return result;} catch (UnsupportedEncodingException e) {e.printStackTrace();}return "";}public static String readFile(String path, String encodeFormat) {try {String context = "";InputStreamReader is;is = new InputStreamReader(new FileInputStream(path), encodeFormat);BufferedReader br = new BufferedReader(is);String line;while ((line = br.readLine()) != null) {context += line + "\r\n";// System.out.println(line);}br.close();return context;} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return "";}/** * 将内容输出为指定格式的文件保存到磁盘上 * * @param context * @param path * @param destEncode */public static void writeFile(String context, String path, String destEncode) {File file = new File(path);if (file.exists())file.delete();BufferedWriter writer;try {FileOutputStream fos = new FileOutputStream(path);writer = new BufferedWriter(new OutputStreamWriter(fos, destEncode));writer.append(context);writer.close();} catch (IOException e) {e.printStackTrace();}}final static void showAllFiles(File dir) throws Exception {File[] fs = dir.listFiles();for (int i = 0; i < fs.length; i++) {if (fs[i].isDirectory()) {try {showAllFiles(fs[i]);} catch (Exception e) {}} else {if (fs[i].getPath().endsWith(".java") || fs[i].getAbsolutePath().endsWith(".xml")) {String filePath;filePath = fs[i].getAbsolutePath();System.out.println(fs[i].getName());//System.out.println(filePath);content = readFile(filePath, "UTF-8");// System.out.println(content);writeFile(content, "c:/test/" + fs[i].getName(), "GBK");}}}}}
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;public class TxtFormatTransfer {public static String DefaultSrcEncodeFormat = "UTF-8";public static String DefaultDestEncodeFormat = "GBK";public static String UnsupportedEncodingExceptionError = "编码格式错误!";public static String FileNotFoundExceptionError = "文件不存在!";public static String IOExceptionError = "文件读写错误!";public static String IsUtf8File = "文件是UTF-8编码格式!";public static String IsNotUtf8File = "文件不是UTF-8编码格式!";public static String readFile(String path, String encodeFormat) {if ((encodeFormat == null || encodeFormat.equals(""))) {if (isUTF8File(path)) {encodeFormat = DefaultDestEncodeFormat;} else {encodeFormat = DefaultSrcEncodeFormat;}}try {String context = "";InputStreamReader isr;isr = new InputStreamReader(new FileInputStream(path), encodeFormat);BufferedReader br = new BufferedReader(isr);String line;while ((line = br.readLine()) != null) {context += line + "\r\n";System.out.println(line);}br.close();return context;} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blockSystem.out.println(UnsupportedEncodingExceptionError);e.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println(FileNotFoundExceptionError);e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println(IOExceptionError);e.printStackTrace();};return "";}/** * Unicode、Unicode big endian和UTF-8编码的txt文件的开头会多出几个字节, * 分别是FF、FE(Unicode),FE、FF(Unicode bigendian),EF、BB、BF(UTF-8) * * @param path * @return */public static boolean isUTF8File(String path) {File file = new File(path);byte[] buf = new byte[3];FileInputStream is = null;try {is = new FileInputStream(file);int read = is.read(buf, 0, 3);System.out.println("" + buf[0] + " " + buf[1] + " " + buf[2]);return false;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {is.close();} catch (IOException e) {e.printStackTrace();}}return false;}public static String transfer(String context, String encodeFormat) {if (encodeFormat == null || encodeFormat.equals(""))encodeFormat = DefaultDestEncodeFormat;try {byte[] content = context.getBytes();String result = new String(content, encodeFormat);return result;} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blockSystem.out.println(UnsupportedEncodingExceptionError);e.printStackTrace();}return "";}public static void writeFile(String context, String path, String destEncode) {File file = new File(path);if (file.exists())file.delete();BufferedWriter writer;try {FileOutputStream fos = new FileOutputStream(path, true);writer = new BufferedWriter(new OutputStreamWriter(fos, destEncode));writer.append(context);writer.close();} catch (IOException e) {System.out.println(IOExceptionError);e.printStackTrace();}}public static void writeFile(String context, String path) {File file = new File(path);if (file.exists())file.delete();Writer writer;try {writer = new FileWriter(file, true);writer.append(context);writer.close();} catch (IOException e) {System.out.println(IOExceptionError);e.printStackTrace();}}public static void transfer(String srcPath, String destPath, String srcEncode, String destEncode) {if (destPath == null || destPath.equals(""))destPath = srcPath;String context = readFile(srcPath, srcEncode);context = transfer(context, destEncode);writeFile(context, destPath, destEncode);}public static void transfer(String srcPath, String destPath, String destEncode) {//if (isUTF8File(srcPath) == false) {transfer(srcPath, destPath, DefaultSrcEncodeFormat, "GBK");//}}public static void main(String args[]) {String path1 = "RetrofitSingleton.java";transfer(path1, path1, "GBK");}}
1 0
- UTF-8转换成GBK
- js中gbk转换成utf-8
- gbk 与 UTF-8 转换
- UTF-8到GBK转换
- javascript使用ado把gbk转换成utf-8代码
- url传值把utf-8转换成gbk
- [Linux系统编程]将GBK转换成UTF-8
- MySQL GBK→UTF-8编码转换
- utf-8转换GB3212或GBK
- iconv转换编码格式 gbk utf-8
- 编码转换:UTF-8 BOM to GBK
- linux C gbk utf-8编码转换
- utf-8和gbk之间相互转换
- GBK与UTF-8之间的转换
- VC++ UTF-8与GBK格式转换
- UTF-8与GBK转换程序
- GBK与UTF-8之间的转换
- GBK,UTF-8批量转换工具
- jQuery ajaxFileUpload文件上传之前端
- 关于Adapter的notifyDataSetChanged()方法数据不更新问题解析
- Spring-AOP-通知-异常通知
- 快速排序理解(挖坑填数策略)
- pip 指定 安装源
- UTF-8转换成GBK
- 微信小程序 image组件的mode属性 以及 图片出现横向滚动轴
- java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I解决方法
- Web服务与I/O模型
- 字符集概述
- 音频驱动之UDA1341芯片之L3接口介绍
- C#代码隐藏li标签
- 后台学习四---el语言及jstl表达式
- Cocos 资料大全 下载链接地址