1 | package hozo.sparql.plugin.compare;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | public class FileUtil {
|
---|
12 |
|
---|
13 |
|
---|
14 | public static List<String> readFileText(File file, String encode){
|
---|
15 | List<String> ret = new ArrayList<String>();
|
---|
16 | try {
|
---|
17 | BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode));
|
---|
18 |
|
---|
19 | String line;
|
---|
20 | line = br.readLine();
|
---|
21 | while (line != null){
|
---|
22 | ret.add(line);
|
---|
23 | line = br.readLine();
|
---|
24 | }
|
---|
25 | } catch (IOException e) {
|
---|
26 | e.printStackTrace();
|
---|
27 | }
|
---|
28 |
|
---|
29 | return ret;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public static List<String> splitLine(String line, String separator){
|
---|
33 | // 繝サ""縺ァ蝗イ縺セ繧後※縺�l縺ー菴輔′縺ゅm縺�′繝ッ繝ウ繧サ繝�ヨ
|
---|
34 | // 繝サ繧サ繝代Ξ繝シ繧ソ繝シ縺ァ蛹コ蛻�i繧後◆縺ィ縺薙m縺ァ蛻�牡
|
---|
35 | // 繝サ""縺ッ蜑企勁縺吶k
|
---|
36 | List<String> ret = new ArrayList<String>();
|
---|
37 | StringBuilder sb = new StringBuilder();
|
---|
38 | boolean literal = false;
|
---|
39 | boolean sep = false;
|
---|
40 |
|
---|
41 | for (int i=0; i<line.length(); i++){
|
---|
42 | String chr = line.substring(i, i+1);
|
---|
43 |
|
---|
44 | if (chr.equals("\"")){
|
---|
45 | sep = false;
|
---|
46 | literal = !literal;
|
---|
47 | } else if (chr.equals(separator) && !literal){
|
---|
48 | sep = true;
|
---|
49 | ret.add(sb.toString());
|
---|
50 | sb = new StringBuilder();
|
---|
51 | } else {
|
---|
52 | sep = false;
|
---|
53 | sb.append(chr);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | if (sb.length() > 0 || sep){
|
---|
57 | ret.add(sb.toString());
|
---|
58 | }
|
---|
59 |
|
---|
60 | return ret;
|
---|
61 | }
|
---|
62 | }
|
---|