java utf-8 bom源码文件为utf-8编码文件

  • 时间:2020-12-13 20:17 编辑: 来源: 阅读:278
  • 扫一扫,手机访问
摘要:有些eclipse开发的项目,idea导入项目,需要把java源文件转换。参考以下:今天在最代码下载的一个项目基于ssm+easyui开发的公司员工后台管理系统,导入IntelliJ IDEA并且设置为java web项目后,编译的时候发现提示错误Error:(1, 1)

有些eclipse开发的项目,idea导入项目,需要把java源文件转换。参考以下:


今天在最代码下载的一个项目基于ssm+easyui开发的公司员工后台管理系统,导入IntelliJ IDEA并且设置为java web项目后,编译的时候发现提示错误

Error:(1, 1) java: 非法字符: 65279
Error:(1, 10) java: 需要class, interface或enum

java脚本批量转换java utf-8 bom源码文件为utf-8编码文件

于是搜索一番发现是因为该java源文件编码是utf-8 bom文件,需要设置为utf-8 无bom文件,于是通过notepad++替换了2个java源码文件

java脚本批量转换java utf-8 bom源码文件为utf-8编码文件

但是发现有几十个java源码文件,这样一个个替换确实太费劲了,于是想到批量转换,暂时没发现notepad++有此类插件,百度上发现editplus可以实现,可以参考https://jingyan.baidu.com/article/dca1fa6f4cea7cf1a5405210.html,另外发现通过java语言也可以实现该功能,代码如下

001package com.javaniu.core.util;
002
003import java.io.*;
004import java.util.ArrayList;
005import java.util.List;
006/**
007 * - changed BOM recognition ordering (longer boms first)
008 * 网络地址:http://koti.mbnet.fi/akini/java/unicodereader/UnicodeReader.java.txt
009 * Original pseudocode   : Thomas Weidenfeller
010 * Implementation tweaked: Aki Nieminen
011 http://www.unicode.org/unicode/faq/utf_bom.html
012 * BOMs:
013 * 00 00 FE FF    = UTF-32, big-endian
014 * FF FE 00 00    = UTF-32, little-endian
015 * EF BB BF       = UTF-8,
016 * FE FF          = UTF-16, big-endian
017 * FF FE          = UTF-16, little-endian
018 * Win2k Notepad:
019 * Unicode format = UTF-16LE
020 ***/
021
022/**
023 * Generic unicode textreader, which will use BOM mark
024 * to identify the encoding to be used. If BOM is not found
025 * then use a given default or system encoding.
026 */
027public class UTF8BOMConverter extends Reader {
028    PushbackInputStream internalIn;
029    InputStreamReader internalIn2 = null;
030    String defaultEnc;
031    private static final int BOM_SIZE = 4;
032
033    /**
034     * @param in         inputstream to be read
035     * @param defaultEnc default encoding if stream does not have
036     *                   BOM marker. Give NULL to use system-level default.
037     */
038    UTF8BOMConverter(InputStream in, String defaultEnc) {
039        internalIn = new PushbackInputStream(in, BOM_SIZE);
040        this.defaultEnc = defaultEnc;
041    }
042
043    public String getDefaultEncoding() {
044        return defaultEnc;
045    }
046
047    /**
048     * Get stream encoding or NULL if stream is uninitialized.
049     * Call init() or read() method to initialize it.
050     */
051    public String getEncoding() {
052        if (internalIn2 == nullreturn null;
053        return internalIn2.getEncoding();
054    }
055
056    /**
057     * Read-ahead four bytes and check for BOM marks. Extra bytes are
058     * unread back to the stream, only BOM bytes are skipped.
059     */
060    protected void init() throws IOException {
061        if (internalIn2 != nullreturn;
062        String encoding;
063        byte bom[] = new byte[BOM_SIZE];
064        int n, unread;
065        n = internalIn.read(bom, 0, bom.length);
066        if ((bom[0] == (byte0x00) && (bom[1] == (byte0x00) &&
067                (bom[2] == (byte0xFE) && (bom[3] == (byte0xFF)) {
068            encoding = "UTF-32BE";
069            unread = n - 4;
070        else if ((bom[0] == (byte0xFF) && (bom[1] == (byte0xFE) &&
071                (bom[2] == (byte0x00) && (bom[3] == (byte0x00)) {
072            encoding = "UTF-32LE";
073            unread = n - 4;
074        else if ((bom[0] == (byte0xEF) && (bom[1] == (byte0xBB) &&
075                (bom[2] == (byte0xBF)) {
076            encoding = "UTF-8";
077            unread = n - 3;
078        else if ((bom[0] == (byte0xFE) && (bom[1] == (byte0xFF)) {
079            encoding = "UTF-16BE";
080            unread = n - 2;
081        else if ((bom[0] == (byte0xFF) && (bom[1] == (byte0xFE)) {
082            encoding = "UTF-16LE";
083            unread = n - 2;
084        else {
085            // Unicode BOM mark not found, unread all bytes
086            encoding = defaultEnc;
087            unread = n;
088        }
089        //System.out.println("read=" + n + ", unread=" + unread);
090        if (unread > 0) internalIn.unread(bom, (n - unread), unread);
091        // Use given encoding
092        if (encoding == null) {
093            internalIn2 = new InputStreamReader(internalIn);
094        else {
095            internalIn2 = new InputStreamReader(internalIn, encoding);
096        }
097    }
098
099    public void close() throws IOException {
100        init();
101        internalIn2.close();
102    }
103
104    public int read(char[] cbuf, int off, int len) throws IOException {
105        init();
106        return internalIn2.read(cbuf, off, len);
107    }
108
109
110    private static void readContentAndSaveWithEncoding(String filePath, String readEncoding, String saveEncoding) throws Exception {
111        saveContent(filePath, readContent(filePath, readEncoding), saveEncoding);
112    }
113
114    private static void saveContent(String filePath, String content, String encoding) throws Exception {
115        FileOutputStream fos = new FileOutputStream(filePath);
116        OutputStreamWriter w = new OutputStreamWriter(fos, encoding);
117        w.write(content);
118        w.flush();
119    }
120
121    private static String readContent(String filePath, String encoding) throws Exception {
122        FileInputStream file = new FileInputStream(new File(filePath));
123        BufferedReader br = new BufferedReader(new UTF8BOMConverter(file, encoding));
124        String line = null;
125        String fileContent = "";
126        while ((line = br.readLine()) != null) {
127            fileContent = fileContent + line;
128            fileContent += " ";
129        }
130        return fileContent;
131    }
132
133    private static List<String> getPerlineFileName(String filePath) throws Exception {
134        FileInputStream file = new FileInputStream(new File(filePath));
135        BufferedReader br = new BufferedReader(new InputStreamReader(file, "UTF-8"));
136        String line = null;
137        List<String> list = new ArrayList<String>();
138        while ((line = br.readLine()) != null) {
139            list.add(line);
140        }
141        return list;
142    }
143
144
145    private static List<String> getAllFilePaths(File filePath, List<String> filePaths) {
146        File[] files = filePath.listFiles();
147        if (files == null) {
148            return filePaths;
149        }
150        for (File f : files) {
151            if (f.isDirectory()) {
152                filePaths.add(f.getPath());
153                getAllFilePaths(f, filePaths);
154            else {
155                filePaths.add(f.getPath());
156            }
157        }
158        return filePaths;
159    }
160
161    public static void main(String[] args) throws Exception {
162        String suffix = ".java";
163
164        List<String> paths = new ArrayList<String>();
165        paths = getAllFilePaths(new File("D:/zuidaima_idea/employee/"), paths);
166
167        List<String> pathList = new ArrayList<String>();
168        for (String path : paths) {
169            if (path.endsWith(suffix)) {
170                pathList.add(path);
171            }
172        }
173
174        for (String path : pathList) {
175            //注意如果是GBK编码的文件,需要2个参数为GBK,否则文件会乱码无法恢复
176            readContentAndSaveWithEncoding(path, "UTF-8""UTF-8");
177            System.out.println(path + "转换成功");
178        }
179    }
180}

可以指定文件夹和后缀,这样程序就可以批量查找到某个文件夹下某种后缀的文件进行编码转换了,执行结果如下图

java脚本批量转换java utf-8 bom源码文件为utf-8编码文件

该项目已经可以在idea下正常编译和运行了

java脚本批量转换java utf-8 bom源码文件为utf-8编码文件


  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【技术文章|】Java 开发环境配置(2021-01-16 19:16)
【技术文章|java技术】Java简介 主要特性 发展历史(2021-01-16 19:10)
【技术文章|java技术】eclipse项目如何导入myeclipse项目中 如何把eclipse项目导入myeclipse(2020-12-20 22:06)
【技术文章|】如何将MyEclipse项目导入eclipse (2020-12-20 22:00)
【技术文章|java技术】如何用eclipse运行myeclipse做的项目(2020-12-20 21:55)
【技术文章|java技术】怎么分辨java框架(2020-12-17 16:27)
【技术文章|】Editplus批量转换java utf-8 bom源码文件为utf-8编码文件(2020-12-13 20:21)
【技术文章|java技术】java utf-8 bom源码文件为utf-8编码文件(2020-12-13 20:17)
【服务项目|】java程序代写计算机毕业设计代做(2020-10-20 16:54)
【热点|国内】第七次全国人口普查“查人”又“查房”,还可自主填报(2020-10-20 14:49)
底部广告
网站首页 | 关于我们 | 广告合作 | 联系我们 | 隐私条款 | 免责声明 | 站点地图
CopyRight 2014-2024 学帮网 |粤ICP备18000800号-4