文件使用完毕后关闭文件
This commit is contained in:
parent
3b22a015a3
commit
a43733cb97
|
|
@ -35,6 +35,8 @@ public class SearcherTest {
|
||||||
String ip = "1.2.3.4";
|
String ip = "1.2.3.4";
|
||||||
long sTime = System.nanoTime();
|
long sTime = System.nanoTime();
|
||||||
String region = searcher.search(ip);
|
String region = searcher.search(ip);
|
||||||
|
//一定关闭
|
||||||
|
searcher.close();
|
||||||
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
|
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
|
||||||
System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
|
System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -81,6 +83,7 @@ public class SearcherTest {
|
||||||
String ip = "1.2.3.4";
|
String ip = "1.2.3.4";
|
||||||
long sTime = System.nanoTime();
|
long sTime = System.nanoTime();
|
||||||
String region = searcher.search(ip);
|
String region = searcher.search(ip);
|
||||||
|
searcher.close();
|
||||||
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
|
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
|
||||||
System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
|
System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -127,6 +130,7 @@ public class SearcherTest {
|
||||||
String ip = "1.2.3.4";
|
String ip = "1.2.3.4";
|
||||||
long sTime = System.nanoTime();
|
long sTime = System.nanoTime();
|
||||||
String region = searcher.search(ip);
|
String region = searcher.search(ip);
|
||||||
|
//此处可以不调用 searcher.close() 因为使用了newWithBuffer没有打开文件
|
||||||
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
|
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
|
||||||
System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
|
System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
|
|
@ -163,6 +163,7 @@ public class Searcher {
|
||||||
handle.seek(0);
|
handle.seek(0);
|
||||||
final byte[] buff = new byte[HeaderInfoLength];
|
final byte[] buff = new byte[HeaderInfoLength];
|
||||||
handle.read(buff);
|
handle.read(buff);
|
||||||
|
handle.close();
|
||||||
return new Header(buff);
|
return new Header(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,8 +178,10 @@ public class Searcher {
|
||||||
final byte[] buff = new byte[len];
|
final byte[] buff = new byte[len];
|
||||||
int rLen = handle.read(buff);
|
int rLen = handle.read(buff);
|
||||||
if (rLen != len) {
|
if (rLen != len) {
|
||||||
|
handle.close();
|
||||||
throw new IOException("incomplete read: read bytes should be " + len);
|
throw new IOException("incomplete read: read bytes should be " + len);
|
||||||
}
|
}
|
||||||
|
handle.close();
|
||||||
|
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
@ -193,9 +196,10 @@ public class Searcher {
|
||||||
final byte[] buff = new byte[(int) handle.length()];
|
final byte[] buff = new byte[(int) handle.length()];
|
||||||
int rLen = handle.read(buff);
|
int rLen = handle.read(buff);
|
||||||
if (rLen != buff.length) {
|
if (rLen != buff.length) {
|
||||||
|
handle.close();
|
||||||
throw new IOException("incomplete read: read bytes should be " + buff.length);
|
throw new IOException("incomplete read: read bytes should be " + buff.length);
|
||||||
}
|
}
|
||||||
|
handle.close();
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue