In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "introduction and use of java input and output". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction and use of java input and output".
In the first part of the java input and output project, you learned that BufferedInputStream is more efficient than FileInputStream in terms of data reading efficiency. But this conclusion is not always true, the code is as follows:
Private static void bufferedInputStreamTest () throws Exception {
FileInputStream in = new FileInputStream (new File ("data"))
BufferedInputStream bin = new BufferedInputStream (in)
Byte [] buf = new byte [1024]
Int readCount =-1
Long totalCount = 0
Long start = System.currentTimeMillis ()
While ((readCount = bin.read (buf))! =-1) {
TotalCount + = readCount
}
Long end = System.currentTimeMillis ()
System.out.println ("read:" + totalCount + "bytes, time consuming:" + (end-start))
Bin.close ()
}
Private static void inputStreamTest () throws Exception {
FileInputStream in = new FileInputStream (new File ("data"))
Byte [] buf = new byte [1024]
Int readCount =-1
Long totalCount = 0
Long start = System.currentTimeMillis ()
While ((readCount = in.read (buf))! =-1) {
TotalCount + = readCount
}
Long end = System.currentTimeMillis ()
System.out.println ("read:" + totalCount + "bytes, time consuming:" + (end-start))
In.close ()
}
Please look at the test results:
1. When buf is 1024 bytes
BufferedInputStream read: 1073741824 bytes, time: 613
FileInputStream read: 1073741824 bytes, time: 1804
2. When buf is 2048 bytes
BufferedInputStream read: 1073741824 bytes, time: 625
FileInputStream read: 1073741824 bytes, time: 1154
3. When buf is 4096 bytes
BufferedInputStream read: 1073741824 bytes, time: 640
FileInputStream read: 1073741824 bytes, time: 770
4. When buf is 8192 bytes
BufferedInputStream read: 1073741824 bytes, time: 549
FileInputStream read: 1073741824 bytes, time: 569
5. When buf is 16384 bytes
BufferedInputStream read: 1073741824 bytes, time: 575
FileInputStream read: 1073741824 bytes, time: 504
6. When buf is 32768 bytes
BufferedInputStream read: 1073741824 bytes, time: 398
FileInputStream read: 1073741824 bytes, time: 396
So the final conclusion is that when the size of buf increases gradually, the efficiency difference between them becomes smaller and smaller; even when the size of buf is 16384, the read efficiency of FileInputStream is slightly higher than that of BufferedInputStream. (the test is conducted under windows8)
The reason is as follows: the windows operating system manages files in terms of clusters, and the native cluster size is 4096 bytes (which can be seen through the chkdsk command). When the buf size is less than 4096, FileInputStream is equivalent to reading the same cluster multiple times (4096 / cache size) (reading to the cluster involves system calls), while there is a default buffer of 8192 nodes inside the BufferedInputStream. When the buf size is less than 4096, it does not involve system calls, but directly obtains data from the memory BUF. When the buf size is greater than 4096, the difference between the two immediately decreases, mainly because of the same number of system calls!
At this point, I believe you have a deeper understanding of "the introduction and use of java input and output". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.