A simple Java program to the utility of excessive: binary file read and write

Source: Internet
Author: User

File I/O Common Files Read and write:

1. Byte stream read/write text file

FileInputStream;

FileOutputStream;

2. Character stream read and write text file

FileReader;

FileWriter;

BufferedReader;

BufferedWriter;

3. Binary read and write files

DataInputStream;

DataOutputStream;

Here we focus on reading and writing binary files:

First, using the byte stream class DataInputStream read and write binary files

The DataInputStream class is a subclass of FileInputStream, which is an extension of the FileInputStream class.

Use the DataInputStream class to read binary files to the FileInputStream class.

Specific procedures:

1) Introduction of related classes

2) construct a data flow object

3) Read the data of the binary file using the method of the data input stream class

Dis.read ();//Read byte data

4) Turn off the data input stream

Dis.close ();//close data input stream

Second, using the byte stream class DataOutputStream write the binary file

The DataOutputStream class is a subclass of FileOutputStream and needs to use the FileOutputStream class.

Specific procedures:

1) Introduction of related classes

2) construct a data output stream object

3) write the data of the binary file using the method of the data output stream class

Out.write (1);//writes the specified byte data to the binary file

4) Turn off the data output stream

Out.close ();

C. Write a Java program to read the Win.ini file in the Windows directory and output its contents

1 ImportJava.io.DataInputStream;2 ImportJava.io.FileInputStream;3 Importjava.io.FileNotFoundException;4 Importjava.io.IOException;5 6 /**7 * Write a Java program to read the Win.ini file in the Windows directory and output its contents8  * @authorAdministrator9  *Ten  */ One  Public classTest041 { A  -      Public Static voidMain (string[] args) { -DataInputStream dis =NULL; the         Try { -             /*creating a binary input stream*/ -dis =NewDataInputStream (NewFileInputStream ("C:\\windows\\win.ini")); -              +             /*loop Read and output information*/ -             inttemp; +              while((Temp=dis.read ())!=-1){ ASystem.out.print ((Char) temp); at             } -}Catch(FileNotFoundException e) { - e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); -         } in         finally{ -             if(dis!=NULL) { to                 Try { + dis.close (); -}Catch(IOException e) { the e.printstacktrace (); *                 } $             }Panax Notoginseng         } -  the     } +  A}

The DataInputStream class is used in conjunction with the DataOutputStream class to read data of the base data type from the stream in a platform-independent manner

The 1.DataInputStream readUTF () method can read strings encoded with utf-8 characters;

The writeUTF () method of 2.DataOutputStream can write a string with utf-8 character encoding;

Test: Copy Picture

1 ImportJava.io.DataInputStream;2 ImportJava.io.DataOutputStream;3 ImportJava.io.FileInputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 7 /**8 * Copy picture (binary byte stream)9  * @authorAdministratorTen  * One  */ A  Public classTest004 { -  -      Public Static voidMain (string[] args) { theFileInputStream FIS =NULL; -FileOutputStream fos =NULL; -DataInputStream dis =NULL; -DataOutputStream dos =NULL; +         Try { -             //Create an input stream +FIS =NewFileInputStream ("D:\\tengyicheng\\timg.jpg"); Adis =NewDataInputStream (FIS); at             //Create an output stream -FOS =NewFileOutputStream ("D:\\tengyicheng\\myfile\\timg.jpg"); -DOS =NewDataOutputStream (FOS); -             //Loop Read Input -             inttemp; -              while(temp = Dis.read ())!=-1){ in Dos.write (temp); -             } to}Catch(IOException e) { + e.printstacktrace (); -         } the         finally{ *             Try { $                 if(fis!=NULL) {Panax Notoginseng fis.close (); -                 } the                 if(fos!=NULL) { + fos.close (); A                 } the                 if(dis!=NULL) { + dis.close (); -                 } $                 if(dos!=NULL) { $ dos.close (); -                 } -}Catch(IOException e) { the e.printstacktrace (); -             }Wuyi         } the  -     } Wu  -}

A simple Java program to the utility of excessive: binary file read and write

Related Article

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.