[Java study notes (116)] binary data reading and writing, random access files, ZIP files

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. Binary data read and write

(I. Overview

       Data in text format is very convenient for testing and debugging, but not as efficient as binary format. The following describes the transmission of binary format data.

(Two) DataOutput and DataInput interface

       These two interfaces are used to write arrays, characters, boolean values ​​and strings in binary format. For example, writeInt always writes an integer as a 4-byte binary number value, no matter how many bits it has. The space used for a given type of value is the same, and the reading and writing speed is faster than parsing text. .

       The DataInputStream class implements the DataInput interface. The methods in this interface can be used. In order to read binary data from a file, the DataInputStream can be combined with a byte source, as shown below:

var in = new DataInputStream(new FileInputStream(“a.txt”));

(3) Random access to files

       The RandomAccessFile class can find or write files anywhere in the file. Disk files are randomly accessed. Use "r" to indicate read in and "rw" to indicate read and write as the second parameter of the constructor to specify read The writing method is as follows:

var in = new RandomAccessFile(“a.txt”, “r”);

       When the file has been opened by RandomAccessFile, the file will not be deleted, and the random access file has a file pointer indicating the position of the next byte to be read or written out. The seek() method can be used to set the pointer The position of the pointer, getFilePointer() method returns the current position of the pointer. This class implements the DataInput and DataOutput interfaces at the same time.

(4) ZIP file

       ZIP files usually store one or more files in a compressed format. Each ZIP file has a header that contains information such as the name of each file and the compression method used. In Java, use ZipInputStream to read the ZIP document. If you need to browse each individual item in the compressed document, use the getNextEntry() method to return a ZipEntry type object describing these items. After each return, you need to call the closeEntry() method. Come to read in the next item, do not close zin before reading in the last item. As follows:

var zin = new ZipInputStream(new FileInputStream(“a.zip”));
ZipEntry entry;
while((entry = zin.getNextEntry()) != null){
    
    
	zin.closeEntry();
}
zin.close();

       When you want to write to the ZIP file, use ZipOutputStream. For each item you want to put in the ZIP file, you must create a ZipEntry object, and use the putNextEntry() method to write out the new file and send the file data to In the output stream. As follows:

var zout = new ZipOutputStream(new FileOutputStream(“test.zip”));
for all files{
    
    
	var ze = new ZipENtry(fileName);
	zout.putNextEntry(ze);
	zout.closeEntry();
}
zout.close();

       A JAR file is just a ZIP file with list items, and you can use JarInputStream and JarOutputStream to read and write list items.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113357595