编码格式批转换,将指定编码转换为设置的编码
来源:互联网 发布:分层数据流程图 编辑:程序博客网 时间:2024/05/29 08:32
将指定目录下所有文件转码
package com.dou361;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;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 CopyFolder {public static void main(String[] args) throws IOException {// 源文件路径String src = "D:\\Android\\adt-bundle-windows-x86_64\\workspace\\eluxue_online";String dest = "C:\\Users\\Administrator\\Desktop";copyFolderUTF8toGBK(new File(src), new File(dest));System.out.println("复制完成");}/*** * 复制工程项目文件夹,过滤里面的Java文件,将GBK其转为UTF-8的编码格式 * * @param src * 原工程项目文件夹的路径 * @param dest * 保存的工程目录路径 * @throws IOException * 复制异常 */public static void copyFolderUTF8toGBK(File filePath, File fileDest)throws IOException {copyFolder("UTF-8","GBK", filePath, fileDest);}/*** * 复制工程项目文件夹,过滤里面的Java文件,将GBK其转为指定的编码格式 * * @param src * 原工程项目文件夹的路径 * @param dest * 保存的工程目录路径 * @throws IOException * 复制异常 */public static void copyFolderGBKtoUTF8(File filePath, File fileDest)throws IOException {copyFolder("GBK", "UTF-8", filePath, fileDest);}/*** * 复制工程项目文件夹,过滤里面的Java文件,将其转为指定的编码格式 * * @param srcCoding * 原编码格式(如GBK) * @param desCoding * 转换的编码格式(如UTF-8) * @param src * 原工程项目文件夹的路径 * @param dest * 保存的工程目录路径 * @throws IOException * 复制异常 */public static void copyFolder(String srcCoding, String desCoding, File src,File dest) throws IOException {if (!src.exists()) {return;}/** 当前目录 */String strdest = dest.getAbsolutePath() + File.separator+ src.getName();File fileDestZi = new File(strdest);/** 创建当前目录 */fileDestZi.mkdirs();File[] files = src.listFiles();for (File file : files) {if (file.isDirectory()) {copyFolder(srcCoding, desCoding, file, fileDestZi);} else {if (file.getName().endsWith(".java")|| file.getName().endsWith(".JAVA")) {/** 获取文件编码 */String charset = srcCoding;/** 编码相同,无需转码 */if (charset.equalsIgnoreCase(desCoding)) {return;}/** 复制内容 */FileInputStream is = new FileInputStream(file);FileOutputStream os = new FileOutputStream(fileDestZi.getAbsolutePath() + File.separator+ file.getName());BufferedReader br = new BufferedReader(new InputStreamReader(is, charset));BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, desCoding));while (br.ready()) {bw.append(br.readLine());bw.newLine();}br.close();bw.close();} else {/** 复制内容 */FileInputStream is = new FileInputStream(file);BufferedInputStream bis = new BufferedInputStream(is);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileDestZi.getAbsolutePath()+ File.separator + file.getName()));byte[] bys = new byte[8192];int len;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();}}}}}
0 0
- 编码格式批转换,将指定编码转换为设置的编码
- python实现批量转换文件编码(批转换编码示例)
- 将字符串转化为指定编码格式的字节
- 将汉字转换为拼音字母(注意编码保存的格式)
- 将字符串的编码格式转换为utf-8
- 将utf8等编码格式转换为shift_jis格式
- 将utf8等编码格式转换为shift_jis格式
- 编码格式的转换
- 如何使用Java代码将GBK编码格式的工程转换为UTF-8编码格式的工程
- 将网页编码换为UTF-8
- 编码转换:怎样将 GB2312 编码的字符串转换为 ISO-8859-1 编码的字符串?
- 将指定文件内容转化为指定编码的字符串
- Myeclipse将Jsp默认编码格式设置为utf-8
- 将编码格式为utf8转换为GBK的一个脚本
- Unity项目脚本编码格式转换为UTF8编码格式
- 将以前的老代码,批量转换为UTF-8编码(可指定)
- eclipse指定编码格式为UTF-8
- myeclipse下如何设置指定类型文件的编码格式
- javascript 利用hash实现行列转置
- Android 网络编程
- Linux网络编程:原始套接字的魔力【续】
- 结合WordCount实例精解Hadoop的数据存储和运算
- sh/bash/csh/Tcsh/ksh/pdksh等shell本质区别
- 编码格式批转换,将指定编码转换为设置的编码
- 观察者模式
- CrackMapExec:域环境渗透中的瑞士军刀
- html5学习记录03:超链接
- 一些用到的SQL语句
- 揭开网络编程常见API的面纱【上】
- OpenCV进行图像相似度对比的几种办法
- Hadoop bin/hadoop namenode -format 时遇到的几个问题
- 课程流恢复步骤和恢复后的地址