-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.java
213 lines (199 loc) · 5.74 KB
/
util.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 汎用クラス
* @author hebo-MAI
* @since 2012/10/13
* @version 1.0
*
*/
public class util {
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
/**
* 呼び出したときの時間をエラー出力に表示する
*/
public static void print_time() {
System.err.print(get_time());
}
public static String get_time() {
Date d = new Date();
DateFormat df = new SimpleDateFormat("[MM/dd HH:mm:ss]");
return df.format(d);
}
/**
* n面ダイスをm回振った時の和を求める
* @param n : ダイスの面数(0からn-1までのn面のダイス)
* @param m : ダイスを振る回数
* @return : ダイスの出た目の和
*/
public static int dice(int n, int m){
int val = 0;
Random rnd = new Random();
for(int i=0; i<m; i++){
val += rnd.nextInt(n);
}
return val;
}
/** 全角数字を半角に変換します。
* http://www7a.biglobe.ne.jp/~java-master/samples/string/HankakuNumberToZenkakuNumber.html
* @param s 変換元文字列
* @return 変換後文字列
*/
public static String zenkakuNumToHankaku(String s) {
StringBuffer sb = new StringBuffer(s);
for (int i = 0; i < sb.length(); i++) {
char c = sb.charAt(i);
if (c >= '0' && c <= '9') {
sb.setCharAt(i, (char)(c - '0' + '0'));
}
}
return sb.toString();
}
/**
* 正規表現regexが文字列targetに含まれるかどうか
* @param target : 正規表現の対象となる文字列
* @param regex : 正規表現の文字列
* @return 含まれるならtrue , それ以外ならfalse
*/
public static boolean isMatched(String target, String regex) {
Pattern p;
Matcher m;
p = Pattern.compile(regex , Pattern.CASE_INSENSITIVE);
m = p.matcher(target);
return m.find();
}
/**
* 文字列が入っていない要素をリストから取り除く
* @param list 入力
* @return 出力
*/
public static ArrayList<String> removeBlankElement(ArrayList<String> list) {
for(Iterator<String> i = list.iterator(); i.hasNext();) {
if (i.next().length() < 2) i.remove();
}
return list;
}
/**
* ファイルの行数をカウントする
* @param responseFile : 行数をカウントするファイル
* @return ファイルの行数
*/
public static int count_line(File responseFile) {
FileInputStream fis;
BufferedReader br = null;
String line = null;
try {
fis = new FileInputStream(responseFile);
br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
} catch (Exception e) {
print_time();
e.printStackTrace();
}
ArrayList<String> list = new ArrayList<String>();
try {
while((line = br.readLine()) != null) {
list.add(line);
}
} catch (Exception e) {
e.printStackTrace();
return -10;
}
return list.size();
}
/**
* ファイルを読み込み、改行ごとに区切ったものをリストに入れて返す
* @param filename : 読み込むファイル
* @return 改行ごとに区切られた文字列のリスト
*/
public static ArrayList<String> file_to_list(File filename) {
FileInputStream fis;
BufferedReader br = null;
String line = null;
try {
fis = new FileInputStream(filename);
br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
} catch (Exception e) {
print_time();
e.printStackTrace();
}
ArrayList<String> list = new ArrayList<String>();
try {
while((line = br.readLine()) != null) {
list.add(line);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return list;
}
public static ArrayList<String> file_to_list(String filename) {
FileInputStream fis;
BufferedReader br = null;
String line = null;
try {
fis = new FileInputStream(filename);
br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
} catch (Exception e) {
print_time();
e.printStackTrace();
}
ArrayList<String> list = new ArrayList<String>();
try {
while((line = br.readLine()) != null) {
list.add(line);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return list;
}
/**
* 入力のリストを、要素ごとに改行した文字列として出力する
* @param list 入力
* @return それぞれ改行された文字列
*/
public static String list_to_string(ArrayList<String> list) {
String str = "";
for(Iterator<String> i = list.iterator(); i.hasNext(); str += LINE_SEPARATOR) {
str += i.next();
}
return str.substring(0, str.length()-LINE_SEPARATOR.length());
}
/**
* 入力文字列を改行文字("\n")に従って分割する
* @param str : 入力する文字列
* @return 文字列のリスト
*/
public static ArrayList<String> separate_string(String str){
ArrayList<String> list = new ArrayList<String>();
String[] strAry = str.split("\\n");
int k = strAry.length;
for(int i=0 ; i<k ; i++){
list.add(strAry[i]);
}
return list;
}
/**
* 改行を表す"\n"を含む文字列の改行を、実際の改行文字と置換する。
* @param str : "\n"を含む入力文字列
* @return "\n"を改行に置換した文字列
*/
public static String replace_new_line(String str) {
Pattern p = Pattern.compile("\\\\n");
Matcher m = p.matcher(str);
if(m.find()) str = m.replaceAll(LINE_SEPARATOR);
return str;
}
}