-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileIO.java
172 lines (158 loc) · 5.8 KB
/
FileIO.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package jtags;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class FileIO
{
public static PrintStream logStream;
static {
try {
FileIO.logStream = new PrintStream(new FileOutputStream("log.txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static String getSimpleFileName(final String fileName) {
char separator = '/';
if (fileName.lastIndexOf(92) > -1) {
separator = '\\';
}
final int start = fileName.lastIndexOf(separator) + 1;
int end = fileName.lastIndexOf(46);
if (end <= start) {
end = fileName.length();
}
return fileName.substring(start, end);
}
public static String getSimpleClassName(final String className) {
final String name = className.substring(className.lastIndexOf(46) + 1);
return name;
}
public static String getSVNRepoRootName(final String url) {
String name = "";
int end;
for (end = url.length() - 1; url.charAt(end) == '/' && end >= 0; --end) {}
if (end >= 0) {
final int start = url.lastIndexOf(47, end);
if (start <= end) {
name = url.substring(start + 1, end + 1);
}
}
return name;
}
public static String[] splitFileName(final String fileName) {
char separator = '/';
if (fileName.lastIndexOf(92) > -1) {
separator = '\\';
}
final int start = fileName.lastIndexOf(separator) + 1;
int end = fileName.lastIndexOf(46);
if (end <= start) {
end = fileName.length() + 1;
}
final String[] names = { fileName.substring(0, start - 1), fileName.substring(start, end) };
return names;
}
public static String readStringFromFile(final String inputFile) {
try {
final BufferedInputStream in = new BufferedInputStream(new FileInputStream(inputFile));
final byte[] bytes = new byte[(int)new File(inputFile).length()];
in.read(bytes);
in.close();
return new String(bytes);
}
catch (Exception e) {
return null;
}
}
public static void writeStringToFile(final String string, final String outputFile) {
try {
final BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
writer.write(string);
writer.flush();
writer.close();
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public static void writeObjectToFile(final Object object, final String objectFile, final boolean append) {
try {
final ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(objectFile, append)));
out.writeObject(object);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public static Object readObjectFromFile(final String objectFile) {
try {
final ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(objectFile)));
final Object object = in.readObject();
in.close();
return object;
}
catch (Exception e) {
return null;
}
}
public static int countLOC(final File file, final String extension) {
int numOfLines = 0;
if (file.isDirectory()) {
File[] listFiles;
for (int length = (listFiles = file.listFiles()).length, i = 0; i < length; ++i) {
final File sub = listFiles[i];
numOfLines += countLOC(sub, extension);
}
}
else if (file.getName().endsWith("." + extension)) {
try {
@SuppressWarnings("resource")
final BufferedReader in = new BufferedReader(new FileReader(file));
while (in.readLine() != null) {
++numOfLines;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return numOfLines;
}
public static String getHtmlPageContent(final String url, final String query, final String charset) throws MalformedURLException, IOException {
final URLConnection connection = new URL(String.valueOf(url) + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
connection.setReadTimeout(10000);
final InputStream response = connection.getInputStream();
final StringBuilder sb = new StringBuilder();
final BufferedInputStream in = new BufferedInputStream(response);
final byte[] bytes = new byte[10000];
for (int len = in.read(bytes); len != -1; len = in.read(bytes)) {
sb.append(new String(bytes, 0, len));
}
in.close();
return sb.toString();
}
public static ArrayList<String> getAllFilesInFolder(final String folder) {
final ArrayList<String> allFiles = new ArrayList<String>();
File[] listFiles;
for (int length = (listFiles = new File(folder).listFiles()).length, i = 0; i < length; ++i) {
final File file = listFiles[i];
if (file.isFile()) {
System.out.println(String.valueOf(file.getName()) + ":" + file.length());
allFiles.add(file.getPath());
}
else {
allFiles.addAll(getAllFilesInFolder(file.getPath()));
}
}
return allFiles;
}
}