In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to read java documents". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Writing files is basically written in single-threaded order, using FileOutputStream on it. However, reading files is not usually read sequentially, in order to achieve the function of reading where you want to read, you usually need RandomAccessFile.
Before we read the file, we first need to know the starting position (offset) and length (length) of the data, so that we can use RandomAccessFile's seek method to move to the starting position of the data, and then read the data.
The basic process is like this, very simple:
The above code looks complete, but an important part of verification is missing. When you give a person something to do, verify that he has done it and how well he has done it. It's the same here.
RandomAccessFile's read (byte b []) method is defined like this (excluding the unimportant doc): reads b.length bytes, puts them in b, and returns the total number of bytes read; when the end of the file is reached and there is no data to read, return-1.
Every detail in the program needs to be paid attention to. So, why is there a return value here?
I told this method how much data I read, and it returned to me. Isn't that sick? No. Because how much data can be read is uncertain, that is, this method does not guarantee that a single call will definitely read b.length bytes of data. One reason is that the file does not have so many bytes to read.
I am not sure if there is any other reason. I randomly read the data within 1G in a 4G file for 40,000 times by a single thread, and the value returned is the same as the required value, but an up to should not guarantee this method, that is, there are other reasons why the method cannot read the required data. Here is a guess, let's just name Mr. Qiao's guess.
Why should I mention this up to, because the description of InputStream's read (byte b []) is more outrageous:
The first sentence says Reads some number of bytes from the input stream, what is this doc, funny? But this is what this method is all about. It really doesn't guarantee that you can read the complete data you want.
One thing is certain, however, and that is the total number of bytes read into the buffer. You can check whether you have read the complete data.
In the actual system: you have to check whether you have read the complete data. Otherwise, your system may crash, and you don't know what the problem is.
So, is there a remedy? the RandomAccessFile method provides another way: this method will not return to you until you read the b.length byte, unless you encounter an exception at the end of the file. This method is more reliable.
The realization of this method is to set a cycle of the original reading method: if I don't finish it at once, I will continue to read it! Until you get to 0 bytes, that is, to the end of the file.
The implementation of this method can verify Mr. Qiao's guess. (if the ordinary read method can guarantee that except for the end of the file, it can return the required data, then there is no need for circular reading, it only needs to be read once to determine whether the count is 0 or not to throw an exception. Therefore, there must be other reasons why the normal read method cannot read the required data.
After using the readFully () method, we only need to handle the exception of EOFException (End of File Exception, which has not read the required data length until the end of the file).
Therefore, the complete read process is as follows: use readFully + check of RandomAccessFile
This is the end of the content of "how to read java File". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.