因为项目开发须要,从svn中checkout代码下来,用ide打开设置为UTF-8编码进行编译。结果却产生了 illegal character: \65279 错误,搞得一头雾水。 java
开始觉得是编码没有选对,调整了并询问原开发人员确认是UTF-8没有问题。上网翻阅资料后才发现,是因为Windows系统开发的编码为UTF-8(BOM)致使,BOM是Byte-Order Mark的意思。一种为了让编辑器自动识别编码。在文件前3个字节加上了EE,BB,BF,但标准的UTF-8(Linux不支持BOM)编码并不会这样作。 apache
因此在Linux下编译Java,问题就这样产生了。 maven
因为文件众多只好简单写程序批量去掉前3个字节了。 编辑器
CleanBom.java ide
package org.noahx.cleanbom; import org.apache.commons.io.IOUtils; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Stack; /** * Created with IntelliJ IDEA. * User: noah * Date: 2/26/13 * Time: 6:07 PM * To change this template use File | Settings | File Templates. */ public class CleanBom { public static void main(String[] args) { //指定查找文件的父目录javafolder File parent = new File("/nautilus/javafolder"); List<File> javaFiles = findJavaFile(parent); int count = 0; for (File javaFile : javaFiles) { if (isBomFile(javaFile)) { count++; cleanBom(javaFile); } } System.out.println("bom=" + count); } /** * 清除bom编码 * * @param file */ public static void cleanBom(File file) { File tempFile = new File(file.getAbsolutePath() + ".tmp"); FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream(tempFile); fis = new FileInputStream(file); fis.read(new byte[3]);//读取前3个byte IOUtils.copy(fis, fos); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fos); IOUtils.closeQuietly(fis); } if (!file.delete()) { System.out.println("Could not delete file"); } if (!tempFile.renameTo(file)) { System.out.println("Could not rename file"); } System.out.println(file.getAbsolutePath() + ">>clean bom"); } /** * 查找子目录下全部java文件 * * @param parent * @return */ public static List<File> findJavaFile(File parent) { List<File> result = new ArrayList<File>(); Stack<File> stack = new Stack<File>(); stack.push(parent); while (!stack.isEmpty()) { File popFile = stack.pop(); if (popFile.isDirectory()) { for (File file : popFile.listFiles()) { stack.add(file); } } else { if (popFile.getName().endsWith(".java")) { result.add(popFile); } } } return result; } /** * 判断是否为bom编码文件 * * @param file * @return */ public static boolean isBomFile(File file) { boolean isBom = false; FileInputStream fileIS = null; try { fileIS = new FileInputStream(file); byte[] bomBytes = new byte[3]; fileIS.read(bomBytes); //EF BB BF if (bomBytes[0] == -17 && bomBytes[1] == -69 && bomBytes[2] == -65) { isBom = true; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fileIS); } return isBom; } }pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cleanbom</groupId> <artifactId>cleanbom</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> </dependencies> </project>
源码下载:http://sdrv.ms/Wp4igi svn