2018-03-21 11:34:44 java脚本批量转换java utf-8 bom源码文件为utf-8编码文件
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编码文件的更多相关文章
- GCN代码分析 2019.03.12 22:34:54字数 560阅读 5714 本文主要对GCN源码进行分析。
GCN代码分析 1 代码结构 . ├── data // 图数据 ├── inits // 初始化的一些公用函数 ├── layers // GCN层的定义 ├── metrics // 评测指标 ...
- 图书管理系统(Java实现,十个数据表,含源码、ER图,超详细报告解释,2020.7.11更新)
图书管理系统数据库设计实验报告 文章目录 更新日志 1.概述 2.需求分析 2.1需要实现的功能 2.2业务流程图 2.2.1学生流程图 2.2.2管理员流程图 2.2.3超级管理员流程图 2.3功能 ...
- java学习笔记(3)数据类型、源码、反码、补码、精度损失、基本数据类型互相转换
关于java中的数据类型: 1.数据类型的作用是什么? 程序当中有很多数据,每一个数据都是有相关类型的,不同数据类型的数据占用的空间大小不同. 数据类型的作用是指导java虚拟机(JVM)在运行程序的 ...
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...
- java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream)
我们以ByteArrayInputStream,拉开对字节类型的“输入流”的学习序幕.本章,我们会先对ByteArrayInputStream进行介绍,然后深入了解一下它的源码,最后通过示例来掌握它的 ...
- Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...
- java基础解析系列(十)---ArrayList和LinkedList源码及使用分析
java基础解析系列(十)---ArrayList和LinkedList源码及使用分析 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder jav ...
- [Spark内核] 第34课:Stage划分和Task最佳位置算法源码彻底解密
本課主題 Job Stage 划分算法解密 Task 最佳位置算法實現解密 引言 作业调度的划分算法以及 Task 的最佳位置的算法,因为 Stage 的划分是DAGScheduler 工作的核心,这 ...
- 【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...
随机推荐
- sp 数据拼接html table表转换xml,发邮件
USE [BES_ADV] GO /****** Object: StoredProcedure [dbo].[RSP_FN_UNAPPLIED_Mail_Reminder] Script Date: ...
- 编程进阶:Java小白的序列化Serializable接口
在之前的学习过程中,我们知道了如何使用FileInputStream输入流和FileOutputStream输出流编写程序读写文件. 下面我们来学习一下如何使用序列化和反序列化读写文件. 一.序列化 ...
- XML入门介绍(什么是XML及XML格式)
什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language). XML 是一种很像HTML的标记语言. XML 的设计宗旨是传输数据,而不是显示数据. XML 标 ...
- 微服务学习笔记二:Eureka服务注册发现
Eureka服务注册发现 服务发现:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移. 1. Service Discovery: Eureka S ...
- String StringBuffer StringBuilder对比
1.相同点 三者都可以用来存储字符串类型数据. 2.不同点 String类型对象内容不可变,每变化一次都会创建一个新的对象. StringBuiler与StringBuffer的内容与长度均可以发生变 ...
- 初入门 HTML
---恢复内容开始--- 1.h标签(标题标签) h1~h62.br标签(换行标签) <br/>3.hr标签(水平线标签) <hr/>4.strong(加粗) em(倾斜)5. ...
- 【阿里云产品公测】小白对OTS两点小建议
作者:阿里云用户荷包蛋 我是大一的新生,作为一个爱技术爱学习爱折腾的熊孩子,我在暑假申请了ECS,学到了很多东西.现在阿里巴巴又开放了很多免费测试,我抱着学习和围观的心态申请了测试,其中有OTS这个高 ...
- CSS3中的Flexbox弹性布局(二)
flexbox详解 flexbox的出现是为了解决复杂的web布局,因为这种布局方式很灵活.容器的子元素可以任意方向进行排列.此属性目前处于非正式标准. flex布局模型不同于块和内联模型布局,块和内 ...
- CNN 和RNN 中input 长度不一致问题
转自:https://www.jianshu.com/p/86d667ee3c62,感谢分享! pad_sequences & Masking layer 上面提到,文本数据也可以用CNN来处 ...
- Hadoop ->> 关于数据分割(Data Split)的思考
今天开始学习Hadoop这门热门的数据库技术.直接从被奉为Hadoop圣经的<Hadoop The Definitive Guide 4th Edition>入手.第一章作者写到一个关于分 ...