2929 * @author zxiaofan
3030 */
3131public class FileUtil {
32- static String max = "max" ;
3332
33+ /**
34+ * 缓存指定数据的偏移位置.
35+ */
3436 static Map <String , Long > mapIndex = new ConcurrentHashMap <>(); // <行key,index>
3537
3638 /**
@@ -48,69 +50,75 @@ public FileUtil() {
4850 *
4951 * @param file
5052 * file
51- * @param content
53+ * @param specialContent
5254 * 查找指定内容
5355 * @return String
5456 * @throws Exception
5557 * Exception
5658 */
57- @ SuppressWarnings ("resource" )
58- public static String getSpecificLine (File file , String content ) throws Exception {
59+ public static String getSpecificLine (File file , String specialContent ) throws Exception {
5960 RandomAccessFile randomAccessFile = null ;
60- randomAccessFile = new RandomAccessFile (file , "r" );
61- long fileLength = randomAccessFile .length (); // 获取当前文件的长度
62- long indexStart = 0 ;
63- if (!mapIndex .containsKey (max )) {
64- mapIndex .put (max , 0L );
65- }
66- Future <String > future = null ;
67- if (mapIndex .containsKey (content )) {
68- indexStart = mapIndex .get (content );
69- } else {
70- indexStart = mapIndex .get (max );
71- // 大文件缓存全部key索引耗时较多,建议按行匹配并发,取优先完成的数据
72- ExecutorService service = Executors .newFixedThreadPool (1 );
73- // future = service.submit(new QueryCallable(file, content));
74- }
75- if (indexStart > fileLength || indexStart < 0 ) {
76- System .out .println ("index illegal" );
77- return null ;
78- }
79- long index = indexStart ;
80- randomAccessFile .seek (index );
81- StringBuffer buffer = new StringBuffer ();
82- String line = null ;
83- boolean getData = false ;
84- while (index <= fileLength && null != (line = randomAccessFile .readLine ())) {
85- line = new String (line .getBytes ("ISO-8859-1" ), "utf-8" ); // 处理乱码
86- String key = line .substring (0 , Math .min (10 , line .length ())); // 假设key为我们要匹配的content
87- if (key .equals (content )) {
88- buffer .append (line );
89- getData = true ;
90- } else if (getData && !key .equals (content )) {
91- break ;
61+ StringBuilder builder ;
62+ try {
63+ randomAccessFile = new RandomAccessFile (file , "r" );
64+ long fileLength = randomAccessFile .length (); // 获取当前文件的长度
65+ long indexStart = 0 ;
66+ if (!mapIndex .containsKey ("max" )) { // 当前最大指针
67+ mapIndex .put ("max" , 0L );
9268 }
93- if (!mapIndex .containsKey (key )) {
94- mapIndex .put (key , index );
69+ Future <String > future = null ;
70+ if (mapIndex .containsKey (specialContent )) {
71+ indexStart = mapIndex .get (specialContent );
72+ } else {
73+ indexStart = mapIndex .get ("max" );
74+ // 大文件缓存全部key索引耗时较多,建议按行匹配并发,取优先完成的数据
75+ ExecutorService service = Executors .newFixedThreadPool (1 );
76+ // future = service.submit(new QueryCallable(file, content));
9577 }
96- if (index > mapIndex .get (max )) {
97- mapIndex .put (max , index );
78+ if (indexStart > fileLength || indexStart < 0 ) {
79+ System .out .println ("index illegal:" + indexStart );
80+ return null ;
9881 }
99- index = randomAccessFile .getFilePointer (); // 获取下次循环的开始指针
100- if (null != future ) {
101- try {
102- String res = future .get (1 , TimeUnit .MILLISECONDS );
103- if (null != res ) {
104- buffer .setLength (0 );
105- buffer .append (res );
106- break ;
82+ long index = indexStart ;
83+ randomAccessFile .seek (index );
84+ builder = new StringBuilder ();
85+ String line = null ;
86+ boolean getData = false ;
87+ while (index <= fileLength && null != (line = randomAccessFile .readLine ())) {
88+ line = new String (line .getBytes ("ISO-8859-1" ), "utf-8" ); // 处理乱码
89+ String key = line .substring (0 , Math .min (10 , line .length ())); // 假设key为我们要匹配的content
90+ if (key .equals (specialContent )) {
91+ builder .append (line );
92+ getData = true ;
93+ } else if (getData && !key .equals (specialContent )) {
94+ break ;
95+ }
96+ if (!mapIndex .containsKey (key )) {
97+ mapIndex .put (key , index );
98+ }
99+ if (index > mapIndex .get ("max" )) {
100+ mapIndex .put ("max" , index );
101+ }
102+ index = randomAccessFile .getFilePointer (); // 获取下次循环的开始指针
103+ if (null != future ) {
104+ try {
105+ String res = future .get (1 , TimeUnit .MILLISECONDS );
106+ if (null != res ) {
107+ builder .setLength (0 );
108+ builder .append (res );
109+ break ;
110+ }
111+ } catch (Exception e ) {
112+ System .out .println ();
107113 }
108- } catch (Exception e ) {
109- System .out .println ();
110114 }
111115 }
116+ } finally {
117+ if (null != randomAccessFile ) {
118+ randomAccessFile .close ();
119+ }
112120 }
113- return buffer .toString ();
121+ return builder .toString ();
114122 }
115123
116124 /**
@@ -129,12 +137,14 @@ public static String readFileToString(String fileName, String encode) throws IOE
129137 try {
130138 File file = new File (fileName );
131139 if (file .exists ()) {
132- if (file .isDirectory ())
140+ if (file .isDirectory ()) {
133141 throw new IOException ("File '" + file + "' exists but is a directory" );
134- if (!file .canRead ())
142+ }
143+ if (!file .canRead ()) {
135144 throw new IOException ("File '" + file + "' cannot be read" );
136- else
145+ } else {
137146 input = new FileInputStream (file );
147+ }
138148 } else {
139149 throw new FileNotFoundException ("File '" + file + "' does not exist" );
140150 }
@@ -145,18 +155,17 @@ public static String readFileToString(String fileName, String encode) throws IOE
145155 } else {
146156 in = new InputStreamReader (input , encode );
147157 }
148- char buffer [] = new char [4096 ];
158+ char [] buffer = new char [4096 ];
149159 for (int n = 0 ; -1 != (n = in .read (buffer ));) {
150160 output .write (buffer , 0 , n );
151161 }
152- String s = output .toString ();
153- return s ;
162+ return output .toString ();
154163 } finally {
155164 if (null != input ) {
156165 try {
157166 input .close ();
158167 } catch (Exception e ) {
159- System . out . print ( "" );
168+ e . printStackTrace ( );
160169 }
161170 }
162171 }
@@ -178,28 +187,33 @@ public static void writeStringToFile(String fileName, String data, String encode
178187 FileOutputStream output = null ;
179188 File file = new File (fileName );
180189 if (file .exists ()) {
181- if (file .isDirectory ())
190+ if (file .isDirectory ()) {
182191 throw new IOException ("File '" + file + "' exists but is a directory" );
183- if (!file .canWrite ())
192+ }
193+ if (!file .canWrite ()) {
184194 throw new IOException ("File '" + file + "' cannot be written to" );
195+ }
185196 } else {
186197 File parent = file .getParentFile ();
187- if (parent != null && !parent .exists () && !parent .mkdirs ())
198+ if (parent != null && !parent .exists () && !parent .mkdirs ()) {
188199 throw new IOException ("File '" + file + "' could not be created" );
200+ }
189201 }
190202 try {
191203 output = new FileOutputStream (file );
192- if (data != null )
193- if (encode == null )
204+ if (data != null ) {
205+ if (encode == null ) {
194206 output .write (data .getBytes ());
195- else
207+ } else {
196208 output .write (data .getBytes (encode ));
209+ }
210+ }
197211 } finally {
198212 if (null != output ) {
199213 try {
200214 output .close ();
201215 } catch (IOException e ) {
202- System . out . print ( "" );
216+ e . printStackTrace ( );
203217 }
204218 }
205219 }
0 commit comments