Java InputStream, OutputStream, reader and writer comparisons

Source: Internet
Author: User

There are several ways to read and write files in Java, where the following comparisons are made by code.

Comparison one: FileWriter and FileReader

 Public classMyCode1 { Public Static voidMain (string[] args) throws IOException {File F=NewFile ("My.txt"); FileWriter FW=NewFileWriter (f); Fw.write ("HELLO");                      Fw.close (); FileReader FR=NewFileReader (f); System. out. println (Char) Fr.read ());                  Fr.close (); }}

Output: H

Visible, FileWriter can be written in batches, but FileReader can only read one character at a time.

Comparison II:outputstream and InputStream

 Public classMyCode1 { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("My.txt"); Char[] data =New Char[]{' H ', ' E ', ' l ', ' l ', ' O '}; OutputStream OS=NewFileOutputStream (f); Os.write (data[0]);                      Os.close (); InputStream is=NewFileInputStream (f); System.out.println ((Char) Is.read ());                  Is.close (); }}

Output: H

Visible,outputstream and InputStream can read and write only one character at a time .

Comparison three:outputstreamwriter and inputstreamreader

 Public classMyCode1 { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("My.txt"); OutputStream OS=NewFileOutputStream (f); OutputStreamWriter OSW=Newoutputstreamwriter (OS); Osw.write ("HELLO");           Osw.close ();                      Os.close (); InputStream is=NewFileInputStream (f); InputStreamReader ISR=NewInputStreamReader (IS); System.out.println ((Char) Isr.read ());           Isr.close ();                  Is.close (); }}

Output: H

Visible,outputstreamwriter can be written in batches, but InputStreamReader can only read one character at a time. Effects with FileWriter and FileReader.

Comparison four:bufferedwriter and bufferedreader

 Public classMyCode1 { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("My.txt"); OutputStream OS=NewFileOutputStream (f); OutputStreamWriter OSW=Newoutputstreamwriter (OS); BufferedWriter BW=NewBufferedWriter (OSW); Bw.write ("HELLO");           Bw.close ();           Osw.close ();                      Os.close (); InputStream is=NewFileInputStream (f); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR);           System.out.println (Br.readline ());           Isr.close ();                  Is.close (); }}

Output: HELLO

Visible,bufferedwriter and BufferedReader can be read and written in bulk .

Java InputStream, OutputStream, reader and writer comparisons

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.