In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 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 "what is the java file format?" in the operation of actual cases, many people will encounter such a dilemma, and 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!
Definition of file format
Wiki definition:
A file format is a standard way that information is encoded for storage in a computer file. It specifies how bits are used to encode information in a digital storage medium.
Translate:
File format is a standard way to encode information and store it in computer files. It specifies how to use bytes to encode information and store it in a digital storage medium.
Human words:
Is to tell you how the data is stored in the file. With the file format, you can write the data into a file in this format and read the data out of the file in this format. The essence of a file format is a protocol.
For example, word, excel, PPT are file formats, they are organized according to their own format, different file suffixes in the computer represent different formats, and the contents of files in the same format can be different.
To develop a file format, you generally need to develop a read-write interface. The simplest write interface is to write a data to a file, and the simplest read interface is to read all the data in it (read here, it generally needs to be read in a format that the user can recognize. Can't the user write an a, you read out a #, and then tell the user that # is a, this is hooliganism.
For another example, word provides an interface for you to write text. Save it off and then open it. He can also show you what was written before, that is, read the file.
The file format generally consists of two parts: data and metadata. Data is data; metadata is metadata (data that describes the data), such as how many pieces of data are in this file, what is the starting position and length of the data.
Here is a description of your own file format:
Implement your own file format!
The function of this file format is to write int data and read all data.
The file structure is shown below: because the file is an one-dimensional structure on disk, the file format is directly drawn as a line, in this file format, there is data in front and metadata at last. Of course, you can also design it in other ways, such as metadata and data cross-storage. Here is only a relatively simple one.
The raw materials are as follows: ByteArrayOutputStream and FileOutputStream are used to write the process. RandomAccessFile is used in the reading process. The metadata here is an int type count that records the number of data in the file. IsWrite is used to determine whether this instance is a read process or a write process. In general, the file format is written first, then closed and then read by another thread.
Two gadgets to convert between int and bytes
Write method: cache the data into baos and count + 1. At this time, only the byte array of the data is put in memory, and the data is not really written to the file.
The close method first writes metadata (count) to the end of the baos, and then writes all the bytes in the baos to the file. Finally, close the file.
Reading method: first read the metadata at the end of the file, and then read the data. As we said above, metadata generally includes the number, location and length of data. But the metadata here has only one number of data. Is there a problem? No problem, because our data is all int, accounting for 4 bytes, so the overall length of the data. And our data is written from the beginning of the file, so the location is known in advance. This is the file format, how to write and read.
Another aspect that can't be ignored is that metadata is actually a kind of data, so how do we read metadata? In fact, metadata also needs its own metadata, so it will be endless, metadta and metadata. However, there is an end, usually at the beginning or end of the file, storing the length of an adjacent metadata. In this way, the first metadata is found. After that, we can find out a series of them. For example, in this case, the termination point is an int, which exists at the end of the file, accounting for 4 bytes.
Main method: first an instance to write the file, and then an instance to read the file. You need to shut down the instance each time you run out of it.
Run result: all the data has been read out
This is the end of the content of "what is the java file format". 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.