SHOW:
|
|
- or go back to the newest paste.
1 | package com.itstep.mvn.app.io; | |
2 | ||
3 | import java.io.Closeable; | |
4 | import java.io.File; | |
5 | import java.io.FileInputStream; | |
6 | import java.io.FileOutputStream; | |
7 | import java.io.FileReader; | |
8 | import java.io.FileWriter; | |
9 | import java.io.IOException; | |
10 | import java.io.InputStream; | |
11 | import java.io.InputStreamReader; | |
12 | import java.io.OutputStream; | |
13 | import java.io.Reader; | |
14 | import java.io.Writer; | |
15 | import java.util.HashMap; | |
16 | import java.util.Map; | |
17 | import java.util.TreeMap; | |
18 | ||
19 | import com.itstep.mvn.app.util.DateTime; | |
20 | ||
21 | public class IOUtils { | |
22 | ||
23 | public static String getFileData(String path) { | |
24 | File file = new File(path); | |
25 | if(!file.exists()) { | |
26 | System.out.println("File does not exists : " + path); | |
27 | return null; | |
28 | } | |
29 | StringBuilder sb = new StringBuilder(); | |
30 | InputStream in = null; | |
31 | InputStreamReader reader = null; | |
32 | try { | |
33 | in = new FileInputStream(path); | |
34 | reader = new InputStreamReader(in, "windows-1251"); | |
35 | int byteOfData; | |
36 | while ((byteOfData = reader.read()) != -1) { | |
37 | char ch = (char) byteOfData; | |
38 | sb.append(ch); | |
39 | }; | |
40 | } catch (IOException e) { | |
41 | e.printStackTrace(); | |
42 | } finally { | |
43 | release(reader, in); | |
44 | } | |
45 | ||
46 | return sb.toString(); | |
47 | ||
48 | } | |
49 | ||
50 | public static void printFile(String path) { | |
51 | File file = new File(path); | |
52 | if(!file.exists()) { | |
53 | System.out.println("File does not exists : " + path); | |
54 | return; | |
55 | } | |
56 | System.out.println("======== PATH: " + path + " ========="); | |
57 | InputStream in = null; | |
58 | try { | |
59 | in = new FileInputStream(path); | |
60 | int byteOfData; | |
61 | while ((byteOfData = in.read()) != -1) { | |
62 | char ch = (char) byteOfData; | |
63 | System.out.print(ch); | |
64 | }; | |
65 | System.out.println("\n================="); | |
66 | } catch (IOException e) { | |
67 | e.printStackTrace(); | |
68 | } finally { | |
69 | release(in); | |
70 | } | |
71 | } | |
72 | ||
73 | public static void writeFile(String path, String txt) { | |
74 | writeFile(path, txt, false); | |
75 | } | |
76 | ||
77 | public static void writeFile(String path, String txt, boolean append) { | |
78 | File file = new File(path); | |
79 | OutputStream out = null; | |
80 | try { | |
81 | if(!file.exists()) | |
82 | file.createNewFile(); | |
83 | out = new FileOutputStream(file, append); | |
84 | if(file.length() != 0) | |
85 | out.write(System.getProperty("line.separator").getBytes()); | |
86 | for( char ch : txt.toCharArray()) { | |
87 | int b = (int)ch; | |
88 | out.write(b); | |
89 | } | |
90 | System.out.println("======== Writing File: " + path + " is DONE ========="); | |
91 | } catch (IOException e) { | |
92 | // TODO Auto-generated catch block | |
93 | e.printStackTrace(); | |
94 | } finally { | |
95 | release(out); | |
96 | } | |
97 | } | |
98 | ||
99 | public static void writeTextStat(Map<String, Integer> stat){ | |
100 | File file = new File("files/stat/STAT_" | |
101 | + DateTime.getCurrentTimeString() + ".txt"); | |
102 | Writer writer = null; | |
103 | try { | |
104 | file.createNewFile(); | |
105 | writer = new FileWriter(file); | |
106 | for (String word : stat.keySet()) { | |
107 | writer.write(word + " -> " + stat.get(word)); | |
108 | writer.write("\n"); // new line | |
109 | } | |
110 | System.out.println("Stats is saved into " | |
111 | + file.getAbsolutePath()); | |
112 | } catch (IOException e) { | |
113 | // TODO Auto-generated catch block | |
114 | e.printStackTrace(); | |
115 | } finally { | |
116 | release(writer); | |
117 | } | |
118 | } | |
119 | ||
120 | ||
121 | public static Map<String, Integer> getTextStat(String path){ | |
122 | Map<String, Integer> statsMap = new HashMap<>(); | |
123 | ||
124 | File file = new File(path); | |
125 | if(!file.exists()) { | |
126 | System.out.println("File path:" + path + " NOT exists!"); | |
127 | } | |
128 | Reader reader = null; | |
129 | try { | |
130 | reader = new FileReader(file); | |
131 | int i; | |
132 | // while the end of the stream has been reached | |
133 | String word = ""; | |
134 | while( (i=reader.read()) != -1){ | |
135 | char ch = (char)i; | |
136 | if(Character.isAlphabetic(ch)) { | |
137 | word += ch; | |
138 | } else if(ch == '\n' || ch == '\r' | |
139 | || Character.isDigit(ch)){ | |
140 | // new line | |
141 | continue; | |
142 | } else { | |
143 | // find word, adding to the map | |
144 | if(word.trim().length() == 0) { | |
145 | continue; | |
146 | } | |
147 | word = word.toLowerCase(); | |
148 | if(statsMap.containsKey(word)) { | |
149 | int count = statsMap.get(word); | |
150 | statsMap.put(word, ++count); | |
151 | } else { | |
152 | statsMap.put(word, 1); | |
153 | } | |
154 | //clean var | |
155 | word = ""; | |
156 | ||
157 | } | |
158 | ||
159 | ||
160 | } | |
161 | ||
162 | } catch (IOException e) { | |
163 | e.printStackTrace(); | |
164 | } finally { | |
165 | release(reader); | |
166 | ||
167 | } | |
168 | // result is sorted | |
169 | return new TreeMap<>(statsMap); | |
170 | ||
171 | } | |
172 | ||
173 | private static void release(Closeable... res) { | |
174 | for (Closeable closeable : res) { | |
175 | if(res != null) { | |
176 | try { | |
177 | closeable.close(); | |
178 | } catch (IOException e) { | |
179 | e.printStackTrace(); | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | } | |
185 | ||
186 | ||
187 | } | |
188 |