Input/output stream

Source: Internet
Author: User
Tags readfile

input : Reading from file to program input read
output : Input from program to file output write

node stream (Raw stream): Pipeline Direct connection Data source
process flow (wrap flow): A process flow that is set above another pipe

Byte Stream bytes are units of character stream characters
InputStream Reader
OutputStream Writer

BYTE stream: Handles all types of data (images, audio ...). )

Character stream: can only handle data of characters (when processing plain text data, the character stream is preferred)

BYTE stream:

inputstream--reading data from an input stream (data source)
Read ()
Inherit InputStream
FileInputStream
StringBufferInputStream
Bytearrayinputstream
Sequenceinputstream
PipedInputStream
FilterInputStream
System.in

 FileInputStream

FileInputStream Read the information in the file as a data source-two ways to read from a file the first, the Chinese characters will appear garbled the second type, no garbled Public classFileinputstreamdemo { Public Static voidMain (string[] args)throwsIOException {Try{FileInputStream ReadFile=NewFileInputStream ("Read.txt"); //The path of this place has a relative path and absolute road strength intIntTemp =Readfile.read ();  while(Inttemp!=-1) {System.out.print (Char) intTemp); IntTemp=Readfile.read ();                    } readfile.close (); }Catch(IOException ex) {}}Try{FileInputStream ReadFile=NewFileInputStream ("Read.txt"); byte[] buffer =New byte[2048]; intByteLength = readfile.read (buffer, 0, 2048); String Str=NewString (buffer, 0, ByteLength);                System.out.println (str);            Readfile.close (); }Catch(IOException ex) {}}

outputstream--writes data to a file from the output data used in the program, and the first supports Chinese. The second one does not support Chinese
Write () flush ()
Flush () and close () first
Inherit OutputStream
FileOutputStream
Bytearrayoutputstream
PipedOutputStream
Fileroutputstream
System.out

  FileOutputStream

 Public Static voidMain (string[] args)throwsIOException {Try{fileoutputstream WriteFile=NewFileOutputStream ("Write.txt");
/* can specify input from keyboard */String s= "Happy new year!happy everyday!" + "\ n" + "Wish fulfillment!" + "\ n" + "Hello \nend of File stream\n"; byte[] bytes =s.getbytes (); Writefile.write (bytes); //Writefile.write (Bytes,0,s.length ()); */Writefile.close (); }Catch(IOException ex) {}Try{fileoutputstream WriteFile=NewFileOutputStream ("Write.txt"); String s= "Happy new year!happy everyday!" + "\ n" + "Wish fulfillment!" + "\ n" + "Hello \nend of File stream\n"; for(inti = 0;i<s.length (); i++) {writefile.write (S.charat (i)); } writefile.close (); }Catch(IOException ex) {}}

Filteredstream

FilterInputStream and Filteroutputstream that extend the functionality provided by the input stream and output stream using the functionality provided by the filtered stream, you must first provide the object to which the filter stream is processed FilterInputStream BufferedinputstreamextendsFilterInputStream requests a usable buffer for the data read to improve the efficiency of the input processing. DataInputStreamextendsFilterInputStream Read the basic type from the stream (int、Char, long, etc.) data. DataInputStream in=NewDataInputStream (NewBufferedinputstream (NewFileInputStream ("C:\\data.txt")  ) ) ; Filtered OutputStream BufferedoutputstreamextendsFilteroutputstream calls Flush () to clear the buffer contents. DataOutputStreamextendsFilteroutputstream writes various basic types of data to the stream. DataOutputStream out=NewDataOutputStream (NewBufferedoutputstream (NewFileOutputStream ("C:\\data.txt"));

Character Stream:

Reader--The stream that inherits from reader is used to enter data into the program, and the data is in the unit of characters (+ bit);
Class FileReader Inheritance InputStreamReader
Class StringReader inherit Reader
Class CharArrayReader Inherit Reader

Use the FileReader class to read the plain text file "Text.txt".  Public Static voidMain (String args[])throwsioexception{Charc[]=New Char[500];//create an array that can hold 500 charactersFileReader R=NewFileReader ("C:\\7\\test.txt");//Create object R        intNum=r.read (c);//reads the data into the character array C and returns the number of characters readString str=NewString (C,0,num);//converts an array of strings into a stringSystem.out.println ("The number of characters read is:" +num+ ", the contents are as follows:");    System.out.println (str); }

Writer--output data to the corresponding output stream (the target of the output), written to the file
Flush ()--Brushes all output streams and outputs all cached bytes to the corresponding output stream.
Class FileWriter Inheritance OutputStreamWriter
Class StringWriter inherit Writer
Class Chararraywriter inherit Writer

Use the FileWriter class to write character arrays and strings into a file.      Public Static void throws ioexception{            FileWriter fw=new FileWriter ("C:\\7\\test2.txt");         Char c[]={' H ', ' e ', ' l ', ' l ', ' o ', ' \ R ', ' \ n '};        String str= "Welcome to java! ";        Fw.write (c);    // writes a character array to a file        Fw.write (str);  // write a string to a file         fw.close ();      }      

Bridging class
InputStreamReader
Convert InputStream to reader;
OutputStreamWriter
Convert OutputStream to writer

BufferedReader
BufferedReader Inherit Reader

BufferedReader stdIn=NewBufferedReader (NewInputStreamReader (system.in)) String input=Stdin.readline (); Reads a row of strings from a file: BufferedReader Filein=NewBufferedReader (Newfilereader (filename)); String Line=Filein.readline ();  while(Line! =NULL) { line=Filein.readline (); }         Public classDemo { Public Static voidMain (string[] args)throwsioexception{Try{BufferedReader Filein=NewBufferedReader (NewFileReader ("C:\\7\\2.txt")); String Line=Filein.readline ();  while(Line! =NULL) {System.out.println (line); Line=Filein.readline (); }                   }Catch(IOException ex) {} }}

BufferedWriter
BufferedWriter Inherits Writer

BufferedWriter writer =NewBufferedWriter (NewFileWriter ("C:\\iodemo.out")) Public Static voidMain (string[] args)throwsioexception{Try{BufferedReader stdin=NewBufferedReader (NewInputStreamReader (system.in)); System.out.print ("Enter a filename:"); String s=Stdin.readline (); BufferedWriter OUT1=NewBufferedWriter (NewFileWriter (s)); System.out.print ("Enter 5 lines of data to end!");  for(intI =0;i<5;i++) {s=Stdin.readline ();                Out1.write (s);            Out1.newline ();              } out1.flush ();        Out1.close (); }Catch(IOException ex) {}}

Input/output stream

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.