Use Java to operate binary files

xiaoxiao2021-03-06  56

in

From the File class, we will introduce the FILE class in detail. This class is very useful, we can use it as a bridge to relax files and streams easier. In the Java IO topic, I am going to introduce some practical about Java IO programming methods, rather than first grasp IO, because I think that effect is not good. When we solved the problems involved in these usual development, we summarize the Java IO system.

When we want to operate files, we must first determine what kind of files we operate, which is a binary file such as a picture or a text file for a character type, which is very important. When we handle binary files, we should use FileInputStream and FileOutputStream, and the processing of text files will be described later.

When we operate file, you can first use the File class to get references to this file, such as file file = new file ("idea.jpg"); then pass the file as a parameter to FileInputStream or FileOutputStream get the corresponding input stream or output flow. Usually we do nothing more than documentation is nothing more than reading, modification, deletion, etc. The most important thing is to read and write operations. When we read the file, you should use the InputStream and use OutputStream when writing files. The READ () method is defined in the InputStream, which is an abstract method. InputStream is of course an abstract class. The examples we have obtained are their subclasses, such as FileInputStream, and subclasses If they are not abstract classes, they must implement the abstract method of the parent class. READ () is not only implemented in FileInputStream, not only two methods for this method provides READ (Byte [] Buffer, INT OFF, INT LENGTH). Let's introduce the following: The READ () method will read the next byte in the input stream and use it as the return value. The return value is between 0-255, and if it is returned to -1, then the end of the file is expressed. Using READ () we can read with one byte and determine the process based on the return value. While ((ch = image.read ())! = - 1) {system.out.print (ch); newfile.write (ch);

READ (Byte [] Buffer reads a range of bytes in the stream into the buffer, the return value is the byte length that actually reads the buffer, if returns -1 indicates that the end of the stream has been reached. While ((ch = image.read (buffer))! = - 1) {system.out.println (ch); newfile.write (buffer);

The meaning of read (byte [] buffer, int off, int future is to write byte the byte of the length of the length of the LENGTH in the buffer of OFF, such as OFF = 7, length = 100, this method Put 100 bytes from the stream to buffer [7] to Buffer [106]. The return value is the byte length that is actually written to the buffer. While ((ch = image.read (buffer, 10,500))! = - 1) {system.out.println (ch); newfile.write (buffer, 10,500);

}

When introducing the above method, we have not considered an abnormal situation, and the reader should refer to the API DOC for the necessary understanding. When we have a flow operation, sometimes we can mark and reset operations, of course, to stream support such operations. Refer to the description of the mark (), reset () and marksupported () method. Finally, after the end of the use, ensure that the flow is turned off, and the close () method is called. Since the FileOutputStream Write-related approach is very similar to the fileinptutstream's read (), no more. Here's an example of how to operate the binary file, we open a JPEG format file, read the content through three different ways, and generate a new file. After running, you will find that these two files are exactly the same! Import java.io. *;

public class LinkFile {public static void main (String [] args) throws IOException {linkBinaryFile ( "Idea.jpg");} private static void linkBinaryFile (String fileName) throws IOException {File imageFile = new File (fileName); if (! ImageFile.exists () &&! imagefile.canread ()) {system.out.println ("can not read the image or the image file doesn't exists"; system.exit (1);} long length = imagefile. Length (); int CH = 0; system.out.println (length); Byte [] buffer = new byte [(int) length / 7]; inputStream Image = new fileInputstream (imagefile); file file = new file (" Hello.jpg "); if (! file.exists ()) {file.createNewFile ();} fileoutputstream newfile = new fileoutputstream (file, true); boolean go = true; while (go) {system.out.println "Please select how to read the file: / n" "1: read () / n2: read (byte [] buffer / N3: read (byte [] buffer, int off, int LEN) / N") BufferedReader Br = New BufferedReader (NEW INPUTSTREADER (SYSTEM.IN)); String line = br.readline (); if (Line.eq UALS ("1")) {while ((ch = image.read ()))! = - 1) {system.out.print (ch); newfile.write (ch);}} else if (line.equals "2"))) {while ((ch = image.read (buffer))! = - 1) {system.ord.Println (ch); newfile.write (buffer);}} else f (line.equals 3 ")) {while ((CH = image.read (buffer, 10,500))! = - 1) {system.out.println (ch); newfile.write (Buffer, 10,500);} for (int i = 0 ; i <10; i ) {system.out.print (buffer [i]);}} go = false;} image.close (); newfile.close ();}}

转载请注明原文地址:https://www.9cbs.com/read-116495.html

New Post(0)