java读取文件并获得文件编码,转换为指定编码的工具类代码
来源:互联网 发布:云南为什么乱 知乎 编辑:程序博客网 时间:2024/04/29 01:04
原文:java读取文件并获得文件编码,转换为指定编码的工具类代码
源代码下载地址:http://www.zuidaima.com/share/1902677591215104.htm
package com.zuidaima.util;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class Test {public static int fileCount = 0;public static String sourceFileRoot = "D:/workspace/test/src/com/zuidaima/"; // 将要转换文件所在的根目录public static String sourceCharset = "gbk"; // 源文件编码public static String targetCharset = "utf8"; // 目标文件编码public static void main(String[] args) throws IOException {File fileDir = new File(sourceFileRoot);convert(fileDir);System.out.println("Total Dealed : " + fileCount + "Files");}public static void convert(File file) throws IOException {// 如果是文件则进行编码转换,写入覆盖原文件if (file.isFile()) {// 只处理.java结尾的代码文件if (file.getPath().indexOf(".java") == -1) {return;}InputStreamReader isr = new InputStreamReader(new FileInputStream(file), sourceCharset);BufferedReader br = new BufferedReader(isr);StringBuffer sb = new StringBuffer();String line = null;while ((line = br.readLine()) != null) {// 注意写入换行符sb.append(line + "\n");}br.close();isr.close();File targetFile = new File(file.getPath() + "." + targetCharset);OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(targetFile), targetCharset);BufferedWriter bw = new BufferedWriter(osw);// 以字符串的形式一次性写入bw.write(sb.toString());bw.close();osw.close();System.out.println("Deal:" + file.getPath());fileCount++;} else {for (File subFile : file.listFiles()) {convert(subFile);}}}}
该代码可以把某个工程的gbk编码java文件全部自动转换为utf-8编码,在工作中很实用。
0 0
- java读取文件并获得文件编码,转换为指定编码的工具类代码
- java读取文件并获得文件编码,转换为指定编码的工具类代码
- java验证文件编码格式工具类(获得任意文件的编码格式)
- .java文件的ANSI编码转换为UTF-8编码
- .java文件的ANSI编码转换为UTF-8编码
- java按照指定编码方式读取文件
- java使用指定编码读取properties文件
- 文件编码转换工具类
- java按指定编码写入和读取文件内容的
- Java版文件编码转换工具
- Java native2ascii工具 properties文件编码转换
- Java native2ascii工具 properties文件编码转换
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- java按指定编码写入和读取文件内容的类
- Nginx + PHP(php-fpm) 安装以及配置
- Python的一些搜集内容
- 37:Linked List Cycle
- Android 单元测试之JUnit
- 面试感悟---一名3年工作经验的程序员应该具备的技能
- java读取文件并获得文件编码,转换为指定编码的工具类代码
- 首次百万量级数据处理
- 欢迎使用CSDN-markdown编辑器
- Android Matrix(变形矩阵)
- Could not find gem 'jquery-rails (~> 3.1.1)
- mac 常用命令
- CVPR 2017-02-16
- Intellij 使用总结
- 微信网页版接口