Advertisement
Guest User

Untitled

a guest
May 28th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. package com.sidecommunity.web.controller;import org.apache.commons.io.IOUtils;
  2.  
  3. import java.io.*;
  4. import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.lang.String;import java.lang.System;import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Stack;
  7.  
  8. /**
  9. * Created by RyanZhu on 4/18/15.
  10. */
  11. public class CleanBom {
  12.  
  13. public static void main(String[] args) throws FileNotFoundException {
  14.  
  15. //指定查找文件的父目录javafolder
  16. File parent = new File("/Users/shuxuan/Documents/github/rongyun-server-sdk-java");
  17.  
  18. List<File> javaFiles = findJavaFile(parent);
  19.  
  20. int count = 0;
  21. for (File javaFile : javaFiles) {
  22. if (isBomFile(javaFile)) {
  23. count++;
  24.  
  25. cleanBom(javaFile);
  26. }
  27.  
  28. }
  29. System.out.println("bom=" + count);
  30.  
  31. }
  32.  
  33. /**
  34. * 清除bom编码
  35. *
  36. * @param file
  37. */
  38. public static void cleanBom(File file) throws FileNotFoundException {
  39.  
  40. File tempFile = new File(file.getAbsolutePath() + ".tmp");
  41.  
  42. FileOutputStream fos = null;
  43. FileInputStream fis = null;
  44. try {
  45. fos = new FileOutputStream(tempFile);
  46. fis = new FileInputStream(file);
  47. fis.read(new byte[3]);//读取前3个byte
  48. IOUtils.copy(fis, fos);
  49.  
  50. } catch (FileNotFoundException e) {
  51. e.printStackTrace();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. } finally {
  55. IOUtils.closeQuietly(fos);
  56. IOUtils.closeQuietly(fis);
  57. }
  58.  
  59. if (!file.delete()) {
  60. System.out.println("Could not delete file");
  61. }
  62.  
  63. if (!tempFile.renameTo(file)) {
  64. System.out.println("Could not rename file");
  65. }
  66.  
  67. System.out.println(file.getAbsolutePath() + ">>clean bom");
  68.  
  69. }
  70.  
  71. /**
  72. * 查找子目录下所有java文件
  73. *
  74. * @param parent
  75. * @return
  76. */
  77. public static List<File> findJavaFile(File parent) {
  78.  
  79. List<File> result = new ArrayList<File>();
  80.  
  81. Stack<File> stack = new Stack<File>();
  82. stack.push(parent);
  83.  
  84. while (!stack.isEmpty()) {
  85.  
  86. File popFile = stack.pop();
  87.  
  88. if (popFile.isDirectory()) {
  89. for (File file : popFile.listFiles()) {
  90. stack.add(file);
  91. }
  92. } else {
  93. if (popFile.getName().endsWith(".java")) {
  94. result.add(popFile);
  95. }
  96. }
  97. }
  98.  
  99. return result;
  100. }
  101.  
  102. /**
  103. * 判断是否为bom编码文件
  104. *
  105. * @param file
  106. * @return
  107. */
  108. public static boolean isBomFile(File file) {
  109. boolean isBom = false;
  110. FileInputStream fileIS = null;
  111. try {
  112. fileIS = new FileInputStream(file);
  113.  
  114. byte[] bomBytes = new byte[3];
  115. fileIS.read(bomBytes);
  116.  
  117. //EF BB BF
  118. if (bomBytes[0] == -17 && bomBytes[1] == -69 && bomBytes[2] == -65) {
  119. isBom = true;
  120. }
  121.  
  122. } catch (FileNotFoundException e) {
  123. e.printStackTrace();
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. } finally {
  127. IOUtils.closeQuietly(fileIS);
  128.  
  129. }
  130. return isBom;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement