情景:接到个任务,需要将某路径下包括子目录下的所有文件都转换为UTF-8格式。
结果:生成FileTransfer.jar,通过批处理调用实现该功能。
没玩过java,先把环境整好吧。
需要以下工具:
JDK安装包:(点击下载)
eclipse:(点击下载)
源码:
import info.monitorenter.cpdetector.CharsetPrinter;
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 EncodeFormatTransfer
{
public static String DefaultSrcEncodeFormat = "GBK";
public static String DefaultDestEncodeFormat = "UTF-8";
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 block
System.out.println(UnsupportedEncodingExceptionError);
e.printStackTrace();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
System.out.println(FileNotFoundExceptionError);
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
System.out.println(IOExceptionError);
e.printStackTrace();
};
return "";
}
public static boolean isUTF8File(String path)
{
try
{
File file = new File(path);
CharsetPrinter detector = new CharsetPrinter();
String charset = detector.guessEncoding(file);
if(charset.equalsIgnoreCase(DefaultDestEncodeFormat))
{
System.out.println(IsUtf8File);
return true;
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(FileNotFoundExceptionError);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(IOExceptionError);
}
System.out.println(IsNotUtf8File);
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 block
System.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(true != isUTF8File(srcPath))
{
transfer(srcPath,destPath,DefaultSrcEncodeFormat,destEncode);
}
}
public static void main(String args[])
{
String path1 = args[0];
transfer(path1,path1,"UTF-8");
}
}
配置环境变量
3个环境变量需要配置:
JAVA_HOME D:\Program Files\Java\jdk1.8.0_05 (这个是JDK安装路径,你安装在哪里这个就填什么路径)
CLASSPATH %JAVA_HOME%\lib
PATH %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; (这个原来就存在,在里面新加上这两个路径就好了)
配置好后,在DOS命令好输入 java -version 出现如下界面表明配置OK
前面下载好的eclipse直接解压就可以使用了,无需安装,双击打开eclipse,创建一个JAVA工程
选择File-->New-->Project
选择Java Project
给工程起个名字,选择JRE8,点击FINISH完成工程创建。
右键点击SRC,New-->Class
给类起个名字,点击FINISH完成类创建,将前面的源码复制粘贴到源文件中。
这里面用到了3个jar包,需要下载加入工程。
cpdetector.jar
jchardet-1.0.jar
antlr.jar
加入工程方法:
选择Libraries,点击Add External JARs...,把选择上面下载的3个包加入工程即可。
把JAVA源码生成为JAR包。
右键点击工程,Export
选择JAVA下的Runnable JAR file
选择源码和生成jar的路径,点击finish就生成JAR包了。
编写批处理:
@echo off
cd "%~dp0"
for /r %%i in (*.py) do java -jar FileTransfer.jar %%i
执行结果如下:
bat批处理文件打包成exe
打开源文件
工程-->选项
选择工程编译,生成exe文件。
最后生成exe文件如下: