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有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...
随机推荐
- C# 在窗体的子线程中创建新窗体
在子线程中如果简单的调用新窗体的话,新出来的窗体会直接一闪而过.没有停留.效果很差 具体解决方法 如下: 在母窗体中建立委托 public delegate void setShowChartForm ...
- C#学习笔记10
1.匿名类型:匿名类型是C#3.0新增的特性,是强类型(由编译器后台在生成为CIL时,自动声明的代码定义类型),声明与初始化属性后其属性是尽读属性.只有在属性的名称.顺序.类型一致时,多个声明匿名变量 ...
- oracle学习篇七:更新操作、事务处理
----------------1.数据库更新操作----------------------------- select * from tab;--查询表 drop table siebel_use ...
- linux系统设置虚拟内存
1. 情景:centos7源码编译mysql5.7.22时,因为服务器内存不足导致编译失败(源码编译至少需要1G内存).报错信息如下: c++: Internal error: Killed (pro ...
- JSP初学者5
JSP中include指令和include动作的区别 include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中, JSP引擎在判断JSP页面未被修改,否则 ...
- Id_Name
<bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean> <be ...
- 快速开始Python/WSGI应用程序
快速开始Python-wsig应用程序 官方参考文档 安装 uwsgi 安装 pip install uwsgi uwsgi --version # 查看 uwsgi 版本 测试 uwsgi 是否正常 ...
- Siebel 集成中的“发布-订阅”与“阅读”
将 Siebel 应用程序中存储的数据提供给企业中的其他应用程序时,通常需要遵循以下两种基本模式之一: 发布-订阅 阅读 “发布-订阅”是一种机制,根据该机制,一个系统(发布者)将更改或更新的数据提供 ...
- SQL Server 的 主键 解决方案 NEWID() , 自增ID
在 SQL Server 表的主键有自增Id ,和 GUID. 1. 自增Id 优点:索引空间小,索引连续.在大量数据插入的时候性能有特别大的优势. 缺点:可移植性差,在数据迁移的时候. 2. G ...
- day014-反射、注解
1. Junit 1.1什么是Junit Junit是Java语言编写的第三方单元测试框架(工具). 1.2单元测试 在Java中,一个类就是一个单元. 单元测试:开发中编写的一小段代码,用来检测类中 ...