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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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<String> listPath()
throws Exception
{
List<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("c:/checkstyle-list.txt"));
String line;
while ((line = reader.readLine()) != null)
{
list.add(line);
}
reader.close();

return list;
}
}