package com.springbootdubbo;

import java.io.*;
import java.util.ArrayList;
import java.util.List; /**
*@title : JavaClass
*@author:zyh
*@createDate:2018/11/19 18:30
*
**/
public class UTF8BOMConverter extends Reader { PushbackInputStream internalIn; InputStreamReader internalIn2 = null; String defaultEnc; private static final int BOM_SIZE = 4; /** * @param in inputstream to be read * @param defaultEnc default encoding if stream does not have * BOM marker. Give NULL to use system-level default. */ UTF8BOMConverter(InputStream in, String defaultEnc) { internalIn = new PushbackInputStream(in, BOM_SIZE); this.defaultEnc = defaultEnc; } public String getDefaultEncoding() { return defaultEnc; } /** * Get stream encoding or NULL if stream is uninitialized. * Call init() or read() method to initialize it. */ public String getEncoding() { if (internalIn2 == null) return null; return internalIn2.getEncoding(); } /** * Read-ahead four bytes and check for BOM marks. Extra bytes are * unread back to the stream, only BOM bytes are skipped. */ protected void init() throws IOException { if (internalIn2 != null) return; String encoding; byte bom[] = new byte[BOM_SIZE]; int n, unread; n = internalIn.read(bom, 0, bom.length); if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) { encoding = "UTF-32BE"; unread = n - 4; } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) { encoding = "UTF-32LE"; unread = n - 4; } else if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) { encoding = "UTF-8"; unread = n - 3; } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { encoding = "UTF-16BE"; unread = n - 2; } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { encoding = "UTF-16LE"; unread = n - 2; } else { // Unicode BOM mark not found, unread all bytes encoding = defaultEnc; unread = n; } //System.out.println("read=" + n + ", unread=" + unread); if (unread > 0) internalIn.unread(bom, (n - unread), unread); // Use given encoding if (encoding == null) {
internalIn2 = new InputStreamReader(internalIn); } else { internalIn2 = new InputStreamReader(internalIn, encoding); } } public void close() throws IOException { init(); internalIn2.close(); } public int read(char[] cbuf, int off, int len) throws IOException { init(); return internalIn2.read(cbuf, off, len); } private static void readContentAndSaveWithEncoding(String filePath, String readEncoding, String saveEncoding) throws Exception { saveContent(filePath, readContent(filePath, readEncoding), saveEncoding); } private static void saveContent(String filePath, String content, String encoding) throws Exception { FileOutputStream fos = new FileOutputStream(filePath); OutputStreamWriter w = new OutputStreamWriter(fos, encoding); w.write(content); w.flush(); } private static String readContent(String filePath, String encoding) throws Exception { FileInputStream file = new FileInputStream(new File(filePath)); BufferedReader br = new BufferedReader(new UTF8BOMConverter(file, encoding)); String line = null; String fileContent = ""; while ((line = br.readLine()) != null) { fileContent = fileContent + line; fileContent += "\r\n"; } return fileContent; } private static List<String> getPerlineFileName(String filePath) throws Exception { FileInputStream file = new FileInputStream(new File(filePath)); BufferedReader br = new BufferedReader(new InputStreamReader(file, "UTF-8")); String line = null; List<String> list = new ArrayList<String>(); while ((line = br.readLine()) != null) { list.add(line); } return list; } private static List<String> getAllFilePaths(File filePath, List<String> filePaths) { File[] files = filePath.listFiles(); if (files == null) { return filePaths; } for (File f : files) { if (f.isDirectory()) { filePaths.add(f.getPath()); getAllFilePaths(f, filePaths); } else { filePaths.add(f.getPath()); } } return filePaths; } public static void main(String[] args) throws Exception { String suffix = ".java"; List<String> paths = new ArrayList<String>(); paths = getAllFilePaths(new File("E:\\mgtt\\DING"), paths); List<String> pathList = new ArrayList<String>(); for (String path : paths) { if (path.endsWith(suffix)) { pathList.add(path);
} } for (String path : pathList) { readContentAndSaveWithEncoding(path, "UTF-8", "UTF-8"); System.out.println(path + "转换成功");
} } }

2018-03-21 11:34:44 java脚本批量转换java utf-8 bom源码文件为utf-8编码文件的更多相关文章

  1. GCN代码分析 2019.03.12 22:34:54字数 560阅读 5714 本文主要对GCN源码进行分析。

    GCN代码分析   1 代码结构 . ├── data // 图数据 ├── inits // 初始化的一些公用函数 ├── layers // GCN层的定义 ├── metrics // 评测指标 ...

  2. 图书管理系统(Java实现,十个数据表,含源码、ER图,超详细报告解释,2020.7.11更新)

    图书管理系统数据库设计实验报告 文章目录 更新日志 1.概述 2.需求分析 2.1需要实现的功能 2.2业务流程图 2.2.1学生流程图 2.2.2管理员流程图 2.2.3超级管理员流程图 2.3功能 ...

  3. java学习笔记(3)数据类型、源码、反码、补码、精度损失、基本数据类型互相转换

    关于java中的数据类型: 1.数据类型的作用是什么? 程序当中有很多数据,每一个数据都是有相关类型的,不同数据类型的数据占用的空间大小不同. 数据类型的作用是指导java虚拟机(JVM)在运行程序的 ...

  4. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  5. java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream)

    我们以ByteArrayInputStream,拉开对字节类型的“输入流”的学习序幕.本章,我们会先对ByteArrayInputStream进行介绍,然后深入了解一下它的源码,最后通过示例来掌握它的 ...

  6. Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...

  7. java基础解析系列(十)---ArrayList和LinkedList源码及使用分析

    java基础解析系列(十)---ArrayList和LinkedList源码及使用分析 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder jav ...

  8. [Spark内核] 第34课:Stage划分和Task最佳位置算法源码彻底解密

    本課主題 Job Stage 划分算法解密 Task 最佳位置算法實現解密 引言 作业调度的划分算法以及 Task 的最佳位置的算法,因为 Stage 的划分是DAGScheduler 工作的核心,这 ...

  9. 【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...

随机推荐

  1. oracle dblink简介

    database link概述 database link是定义一个数据库到另一个数据库的路径的对象,database link允许你查询远程表及执行远程程序.在任何分布式环境里,database都是 ...

  2. jsp的动作标签

    常用的标签: 1. forward   请求转发 [基本不使用] <==> request.getRequestDispatcher(url).forward(request,respon ...

  3. IO流之IO的异常处理

    如果发生了IO的异常.我们在实际开发中,对异常时如何处理的,我们来演示一下. public class FileOutputStreamDemo3 { public static void main( ...

  4. Ubuntu16.04安装Docker1.12+开发实例+hello world+web应用容器

    本次主要是详细记录Docker1.12在Ubuntu16.04上的安装过程,创建Docker组(避免每次敲命令都需要sudo),Docker常用的基本命令的总结,在容器中运行Hello world,以 ...

  5. windows常用命令集锦

    开始→运行→输入的命令集锦 gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 lo ...

  6. 签名&加密的区别

    https://www.zhihu.com/question/27669212/answer/38037256 就拿A给B发送经过签名加密信息来说: 1.A对信息签名的作用是确认这个信息是A发出的,不 ...

  7. 【Leetcode】【Easy】Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. 极点五笔词库DIY

    2004年没啥好的拼音输入法,试了清华紫光输入法一段时间,也相当不满意, 于是在2005年开始学五笔,很快就选定极点五笔了, 使用过程中没啥不满意的,反而还有惊喜: 重装系统后,双击就安装好输入法了, ...

  9. 构建高性能插件式Web框架

    基于MVC插件模式构建支持数据库集群.数据实时同步.数据发布与订阅的Web框架系统.如下图: 1.基于插件式开发 采用插件模式开发的优点是使得系统框架和业务模式有效地进行分离,系统更新也比较简单,只需 ...

  10. 设计模式——代理模式(Proxy Pattern)

    代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. UML图: 模型设计: Subject类: package com.cnblog.clarck; /** * Subject 类 ...