处理UTF-8格式的java文件中的多余空行的checkstyle问题

为什么80%的码农都做不了架构师?>>>   hot3.png

package test;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;

import java.util.ArrayList;
import java.util.List;


public class RemoveBlacket
{
  public static void main(String[] args)
      throws Exception
  {
    for (String file : listPath())
    {
      System.out.println(file);
      proces(file);
    }
    //    test();
  }


  static void test()
      throws Exception
  {
    String file = "C:\\Temp.java";
    FileInputStream in = new FileInputStream(file);
    byte[] bytes = new byte[81920000];
    int len = in.read(bytes);
    String content = new String(bytes, 0, len, "UTF-8"); // 以UTF-8格式处理内容
    content = content.replaceAll("\r\n(\r\n\\s+\\})", "$1");
    in.close();

    FileOutputStream out = new FileOutputStream("C:/text.java");
    //    OutputStreamWriter writer = new OutputStreamWriter(out);
    System.out.println(content);
    out.write(content.getBytes("UTF-8"));; // 还原UTF-8格式内容

    //    writer.close();
    out.close();

  }


  static void proces(String file)
      throws Exception
  {
    FileInputStream in = new FileInputStream(file);
    byte[] bytes = new byte[80920000];
    int len = in.read(bytes);
    String content = new String(bytes, 0, len, "UTF-8");// 以UTF-8格式处理内容
    content = content.replaceAll("\r\n(\r\n\\s+\\})", "$1");
    in.close();

    FileOutputStream out = new FileOutputStream(file);
    //    System.out.println(content);
    out.write(content.getBytes("UTF-8"));// 还原UTF-8格式内容

    out.close();
  }


  static List listPath()
      throws Exception
  {
    List list = new ArrayList();
    BufferedReader reader = new BufferedReader(new FileReader("c:/checkstyle-list.txt"));
    String line;
    while ((line = reader.readLine()) != null)
    {
      list.add(line);
    }
    reader.close();

    return list;
  }
}

转载于:https://my.oschina.net/l1z2g9/blog/15044

你可能感兴趣的:(处理UTF-8格式的java文件中的多余空行的checkstyle问题)