一般用UE或记事本编辑过的UTF-8的文件头会加入BOM标识,该标识由3个char组成。在UTF-8的标准里该BOM标识是可有可无的,Sun 的javac 在编译带有BOM的UTF-8的格式的文件时会出现“非法字符:\65279”的错误,但是用Eclipse进行编译却没有问题,原因在于Eclipse使用的是自己的JDT,而非javac,关于JDT的描述可以到eclipse的官网上去查看。
出现这种情况时,网上提供了两种解决方法:
a) 利用UE、Editplus等文本编辑器重新保存文件为不带BOM的UTF-8格式
b) 可使用eclipse自带的编译器进行编译
<!--在ant中添加property,使用eclipse jdt compiler编译-->
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" />
相关jar包,在eclipse安装文件plugins目录下:org.eclipse.jdt.core_xxx.jar,xxx为版本号
但对于第一种方法,如果文件过多,很显然不可行
对于第二种,我试了好几次也没有成功。
这里提供我的方式,采用程序的方式,编译前将带有utf-8标记的文件的前三个标记字符去掉。
ant代码:
<target name="build.all" depends="svnGet,utf8">
<echo>
先清空目标路径 [${application}] 下的旧文件
</echo>
<delete dir="${application}" includeemptydirs="true" />
<mkdir dir="${javac.dir}" />
<echo>
现在开始编译......,
源文件路径是:${temp.dir}
编译后的类路径是:${application}
</echo>
<javac target="1.5" encoding="utf-8" srcdir="${src.dir}" destdir="${javac.dir}" debug="on" memoryMaximumSize="256m">
<classpath>
<fileset dir="${javac.lib}" />
</classpath>
</javac>
<echo>
编译完毕!
</echo>
</target>
<!-- 清除utf-8标记 -->
<target name="utf8">
<echo>清除utf-8标记</echo>
<java dir="./bin" classname="UTF8Parser" fork="true" failonerror="true" maxmemory="128m">
<arg line="${basedir} ${src.dir}" />
</java>
</target>
提供的两个参数主要是为了指定java文件路径
UTF8Parser.java
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class UTF8Parser {
public static void main(String[] args) {
if (args.length < 2) {
return;
}
if (args[1].startsWith(".")) {
args[1] = args[1].substring(1);
}
File file = new File(args[0] + args[1]);
System.out.println("java代码位置:" + file.getAbsolutePath());
UTF8Parser.clearUTF8Mark(file);
}
private static void clearUTF8Mark(File file) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
clearUTF8Mark(f);
}
} else {
FileInputStream fis = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
OutputStream out = null;
try {
fis = new FileInputStream(file);
is = new BufferedInputStream(fis);
baos = new ByteArrayOutputStream();
byte b[] = new byte[3];
is.read(b);
// System.out.println(b[0] + ":" + b[1] + ":" + b[2]);
if (-17 == b[0] && -69 == b[1] && -65 == b[2]) {
System.out.println(file.getAbsolutePath());
b = new byte[1024];
while (true) {
int bytes = 0;
try {
bytes = is.read(b);
} catch (IOException e) {
}
if (bytes == -1) {
break;
}
baos.write(b, 0, bytes);
b = baos.toByteArray();
}
// System.out.println(new String(b, "utf-8"));
file.delete();
out = new FileOutputStream(file);
baos.writeTo(out);
}
} catch (Exception e) {
System.exit(0);
} finally {
try {
if (fis != null) {
fis.close();
}
if (out != null) {
out.close();
}
if (is != null) {
is.close();
}
if (baos != null) {
baos.close();
}
} catch (Exception e) {
System.exit(0);
}
}
}
}
}
分享到:
相关推荐
NULL 博文链接:https://yzz9i.iteye.com/blog/1580020
标签:ant-commons-logging-1.7.0.jar,ant,commons,logging,1.7.0,jar包下载,依赖包
$ npm install @ant-design/dark-theme 用法 import darkTheme from '@ant-design/dark-theme' ; // webpack.config.js: less-loader { loader : 'less-loader' , options : { modifyVars : darkTheme , } , } ,...
apache-ant-1.6.5-bin ant编译
标签:ant-commons-net-1.9.4.jar,ant,commons,net,1.9.4,jar包下载,依赖包
apache-ant-1.9.16-bin.zip
ant-design-mobile-chart 说明 ant-design-mobile-chart 是为移动端开发的 react 图表组件库,图形语法底层基于 实现图形语法。 组件库具有轻量、灵活、易用的特点 安装 $ npm install ant-design-mobile-chart --...
例如解压到E:\学习类\apache-ant-1.8.2, 2、配置环境变量。在系统环境变量中新建一个ANT_HOME 的变量,值为D:\apache-ant-1.10.1。在path 变量中添加 %ANT_HOME%\bin; 如果没有path变量,则新建一个。 3、验证ant...
English |蚂蚁设计移动RN 可配置的...pod installyarn ios # or yarn android安装与使用$ npm install @ant-design/react-native --save 要么yarn add @ant-design/react-native安装对等依赖项(4.0.0+) npm inst
传单蚂蚁路径创建带有“蚂蚁路径”动画通量的传单折线正在安装通过NPM: npm install --save leaflet-ant-path通过纱线: yarn add leaflet-ant-path通过凉亭: bower install leaflet-ant-path或仅此源代码要求单张...
安装npm install -S @jswork/react-ant-dropdown-menu特性姓名类型必需的默认描述班级名称细绳错误的-- menuOptions 形状错误的--用法导入css @import " ~@jswork/react-ant-dropdown-menu/dist/style.css " ;...
Ant Design Pro Plus官方说明请参见原仓库名称ant design pro v2 plus ,代码移到分支 。重命名为ant design pro plus后,在master分支跟进ant design pro中的更新。注意:预览由于是部署到Github Pages,配置了 ,...
适用于企业应用程序的即用型UI解决方案,作为Vue样板。 基于 预览: : 主页: : 文档: : 变更日志: : 常见问题解答: : 总览 环境和依赖 节点 纱 网络包 埃斯林特 @ vue / cli〜3 -Vue的蚂蚁设计 图片...
ant中使用jmeter task,需要的“ant-jmeter-1.1.1.jar”jar包 放到ant的classpath下就好了
yarn add ant-design-draggable-modal 注意:您必须使用react@16.8.0和react-dom@16.8.0或更高版本。 :hammer: 用法 import React , { useState , useCallback } from 'react' import { Button } from 'antd' ...
apache-ant-1.10.12-bin.tar.gz
apache-ant-1.9.2-bin.zip
开发工具 ant-launcher-1.9.6开发工具 ant-launcher-1.9.6开发工具 ant-launcher-1.9.6开发工具 ant-launcher-1.9.6开发工具 ant-launcher-1.9.6开发工具 ant-launcher-1.9.6开发工具 ant-launcher-1.9.6开发工具 ...
标签:ant-apache-bcel.jar.zip,ant,apache,bcel,jar.zip包下载,依赖包
ant 安装包 新版本 可以尝试下 官网版本 apache-ant-1.10.3-bin.zip