Java file stream: Byte stream (FileInputStream, FileOutputStream) and character stream (FileReader, FileWriter).

Source: Internet
Author: User
Tags sin

The input and output of Java is based on 4 abstract classes: InputStream, OutputStream, Reader, Writer. Inputsream and OutputStream are designed as byte-stream classes, while reader and writer are designed as character stream classes. In general, you should use a character stream class when working with characters or strings, and you should use a byte stream class when working with bytes or binary objects.

Generally in the operation of the file stream, whether it is a byte stream or a character stream, can be done in the following manner.

1. Use the file class to find a document

2. Instantiate a byte stream or a stream of characters through the file class

3. Byte (character) read and write operation

4. Close file stream

Here are 2 examples of writing a string to a file and reading it.

By byte-throttling mode:

1  Packagezdxtest;2 ImportJava.io.*;3  Public classTestFile1 {4 5      Public Static voidMain (string[] args) {6         //TODO auto-generated Method Stub7         //byte stream input output, write content to a file and read8File f =NewFile ("D:\\1.txt");9FileOutputStream fos =NULL;TenFileInputStream foi =NULL; OneString sin = "Today is the last day of July"; A         //Create the file, if the file exists under the directory, first delete and then create -         if(F.exists ()) { - F.delete (); the             Try { - f.createnewfile (); -}Catch(IOException e) { -                 //TODO auto-generated Catch block + e.printstacktrace (); -             }     +         } A         Else{ at             Try { - f.createnewfile (); -}Catch(IOException e) { -                 //TODO auto-generated Catch block - e.printstacktrace (); -             } in         } -         //instantiate byte stream subclass: FileOutputStream to         Try { +FOS =NewFileOutputStream (f); -}Catch(FileNotFoundException e) { the             //TODO auto-generated Catch block * e.printstacktrace (); $         }Panax Notoginseng         //writes a file in bytes and closes the stream -         byte[] B =sin.getbytes (); the         Try { + Fos.write (b); A fos.close (); the}Catch(IOException e) { +             //TODO auto-generated Catch block - e.printstacktrace (); $         } $         //instantiate byte stream subclass: FileInputStream -         Try { -FOI =NewFileInputStream (f); the}Catch(FileNotFoundException e) { -             //TODO auto-generated Catch blockWuyi e.printstacktrace (); the         } -         //reads the file in bytes and closes the stream Wu         byte[] C =New byte[1000]; -         intL=0; About         Try {         $             /*L = foi.read (); - While (l!=-1) { - System.out.println (string.valueof (L)); - L=foi.read (); A             }*/ +L =Foi.read (c); the foi.close (); -}Catch(IOException e) { $             //TODO auto-generated Catch block the e.printstacktrace (); the         } theString o =NewString (c); theSystem.out.println (o);//Print File Contents -System.out.println (l);//number of bytes to print read in  the     } the  About}

Post-run output:

Today is the last day of July

27

Why is it 27 bytes, because my default encoding for Eclipse is UTF-8, in UTF-8 encoding, a Chinese character is three bytes, a total of 9 Chinese characters, so it is 27 bytes.

Then look at the way the character stream is implemented:

1  Packagezdxtest;2 ImportJava.io.*;3 4  Public classFileTest2 {5 6      Public Static voidMain (string[] args) {7         //TODO auto-generated Method Stub8File f =NewFile ("D:\\2.txt");9FileReader FR =NULL;TenFileWriter FW =NULL; OneString s = "Tomorrow is the first day of August"; A         //Create the file, if the file exists under the directory, first delete and then create -         if(F.exists ()) -         { the F.delete (); -             Try { - f.createnewfile (); -}Catch(IOException e) { +                 //TODO auto-generated Catch block - e.printstacktrace (); +             } A         } at         Else{ -             Try { - f.createnewfile (); -}Catch(IOException e) { -                 //TODO auto-generated Catch block - e.printstacktrace (); in             } -         } to         //instantiate character Stream subclass: FileWriter, writes the file as characters and closes the stream +         Try { -FW =NewFileWriter (f); the Fw.write (s); * fw.close (); $}Catch(IOException e) {Panax Notoginseng             //TODO auto-generated Catch block - e.printstacktrace (); the         } +         //instantiate character Stream subclass: FileReader, reads the file as characters and closes the stream A         Char[] B =New Char[1000]; the         intL = 0; +         Try { -FR =NewFileReader (f); $L =Fr.read (b); $}Catch(IOException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); the         } -System.out.println (NewString (b));//Print File ContentsWuyiSystem.out.println (l);//number of characters to print read the     } -  Wu}

Run output:

Tomorrow is the first day of August.

8

Altogether 8 kanji, one char stores a kanji, so it is 8 characters

Java file stream: Byte stream (FileInputStream, FileOutputStream) and character stream (FileReader, FileWriter).

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.