Java??Ч????????
???????????? ???????[ 2015/1/20 10:25:07 ] ??????????????? java
????1??????
??????????????????Java??Ч????????????????????Baeldung ??http://www.baeldung.com/?? ??“Java——??????”??н?????????
????2????????ж??
???????????е??????????????ж????Guava ??Apache Commons IO???????????????????????е??????
????Files.readLines(new File(path)?? Charsets.UTF_8);
????FileUtils.readLines(new File(path));
?????????????????????????????????ж????????????У????????????????3??????OutOfMemoryError ????
???????磺?????????1G???????
????@Test
????public void givenUsingGuava_whenIteratingAFile_thenWorks() throws IOException {
????String path = ...
????Files.readLines(new File(path)?? Charsets.UTF_8);
????}
?????????????????ú??????棺???????????0Mb??棩
????[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Total Memory: 128 Mb
????[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Free Memory: 116 Mb
?????????????????????????к???????????????????????2GB??棩??
????[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Total Memory: 2666 Mb
????[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Free Memory: 490 Mb
????????ζ??????????????2.1GB?????——?????????????????????ж????洢??????С?
????????????е??????????????к????????????——??????????????ж????????????????
???????????????????????????????????????????????——????????????????????????У????????????????????????????????????????????????????——????е?????????????????ж?????????С?
????3???????
???????????????????????????——????????java.util.Scanner???????????????????????????????
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream?? "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
// System.out.println(line);
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
|
??????????????????????е???????——????????н??д????????????????????á??????а?????????????У????????????150MB??棩
????[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Total Memory: 763 Mb
????[main] INFO org.baeldung.java.CoreJavaIoUnitTest - Free Memory: 605 Mb
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11