Use Java to operate binary files

Source: Internet
Author: User
Title: Introduction to Io systems starting from File
Author: mingjava
Last Updated: 0:14:34
5 "href =" http://www.j2medev.com/Article_Show.asp? ArticleID = 79 "target =" _ blank "> the file class is described in detail in the introduction to the IO system starting from the file class. This class is very useful. We can use it as a bridge to easily link files and streams. In the Java I/O topic, I plan to introduce some practical Java I/O programming methods first, instead of grasping the overall I/O, because I think the effect is not good. After solving the problems involved in normal development, let's summarize the Java I/O system.

When we want to operate a file, we must first determine what type of file we operate on, whether it is a binary file sample or a character-type text file, which is very important. When processing binary files, we should use fileinputstream and fileoutputstream to process text files.

When operating on a file, we can first use the File class to get reference to this file, for example
File file = new file ("idea.jpg"); then pass the file as a parameter to fileinputstream or fileoutputstream to get the corresponding input stream or output stream. Generally, you can read, write, modify, or delete an object. The most important thing is read/write operations. When reading files, we should use inputstream and outputstream when writing files. The read () method is defined in inputstream and is an abstract method. Inputstream is also an abstract class. The instance we get is its subclass. For example, fileinputstream. If the subclass is not an abstract class, the abstract method of the parent class must be implemented. In fileinputstream, not only read () is implemented, but also the read (byte [] buffer) and read (byte [] buffer, int off, int length) methods are provided. The following describes in detail:

The read () method reads the next byte in the input stream and uses it as the return value. The returned value is between 0 and. If the returned value is-1, it indicates the end of the file. With read (), we can read data in one byte and judge and process the data based on the returned value.
While (CH = image. Read ())! =-1)
{
System. Out. Print (CH );
Newfile. Write (CH );
}

Read (byte [] buffer) reads certain bytes in the stream into the buffer. The returned value is the length of the bytes actually read into the buffer. If-1 is returned, it indicates that it has reached the end of the stream.
While (CH = image. Read (buffer ))! =-1)
{
System. Out. println (CH );
Newfile. Write (buffer );
}

Read (byte [] buffer, int off, int length) means to write the bytes of length in the stream into the buffer with the off offset, for example, off = 7, when length = 100, this method reads 100 bytes from the stream and puts them in buffer [7] to buffer [106. The returned value is the length of the bytes actually written into 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 did not consider exceptions. Readers should refer to the API Doc for necessary understanding. When we perform streaming operations, we can mark and reset streaming operations. Of course, we need to allow streaming to support such operations. Refer to the descriptions of mark (), reset (), and marksupported () methods. After use, make sure to close the stream and call the close () method. Since the write-related methods of fileoutputstream are very similar to the read () method of fileinptutstream, we will not talk about them more. The following example shows how to operate a binary file. Open a jpeg file, read the content in 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 inputstreamreader (system. In ));
String line = Br. Readline ();
If (line. Equals ("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. Out. println (CH );
Newfile. Write (buffer );
}

}
Else if (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 ();



}
 
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.