Section 40th: IO knowledge case in Java

Source: Internet
Author: User
Tags readline

A stream is a string of characters, a channel of information, a stream of output, and an input stream.

IOThe classification

First: The input stream and the output stream.
The second kind: byte stream and character stream.
Third: node flow and processing flow.

JavaMid-Stream Classification:
The movement direction of the flow can be divided into two types: input stream and output stream.

The data type of the stream, which can be divided into byte stream and character stream.

The input stream classes are InputStream subclasses of abstract classes (byte input streams) or abstract class Reader classes (character input streams).

The output stream classes are OutputStream subclasses of abstract classes (byte output streams) or abstract class Writer classes (character output streams).

Input stream from file input to read, output stream from file output to write data.

Byte input stream InputStrem and sub-section output streamOutputStream

InputStream:FileInputStream,ObjectInputStream,ByteArrayInputStream,StringBufferInputStream,BufferedInputStream,DataInputStream

OutputStream:FileOutputStream,ObjectOutputStream,ByteArrayOutputStream,BufferedOutputStream,DataOutputStream

Input stream
The input stream is used to read data, and the user can read the data from the input stream, but not write the data.

The input stream reads the data as follows:
(1) Open a stream.
Such as:FileInputStream inputFile=new FileInputStream("数据源");
(2) Read the information from the information source.
Such as:inputFile.read();
(3) Close the stream.
Such as:inputFile.close();

Output stream
The output stream is used to write data. Can only write, cannot read.

The process of writing data to the output stream is as follows:
(1) Open a stream.
Such as:FileOutputStream outFile=new FileOutputStream("数据源");
(2) Write the information to the destination.
Such as:outFile.write(inputFile.read()):
(3) Close the stream.
Such as:outFile.close();

Character Stream Reader andWriter

Byte character conversion stream:
InputStreamReader
OutputStreamWriter

Adding buffered streams can speed up the input and output.

FileInputStream fis = new FileInputStream("from.txt");InputStreamReader isr = new InputStreamReader(fis);// 可以加快输入和输出的速度BufferedReader br = new BufferedReader(isr);
FileInputStream fis = new FileInputStream("from.txt");InputStreamReader isr = new InputStreamReader(fis);// 可以加快输入和输出的速度BufferedReader br = new BufferedReader(isr);FileOutputStream fos = new FileOutputStream("from.txt");OutputStreamWriter osw = new OutputStreamWriter(fos);// 可以加快输入和输出的速度BufferedWriter bw = new BufferedWriter(osw);
import java.io.*;class Test{ public static void main(String[] args){  // 声明输入流  FileInputStream fis = null;  // 声明输出流  FileOutputStream fos = nulll;  try{   // 生成输入流对象   fis = new FileInputStream("e:/from.txt");   // 生成输出流的对象   fos = new FileOutputStream("e:/to.txt");  // 生成字节数组   byte[] buffer = new byte[1024]; while(true){ // 调用输入流对象的read方法  int temp = fis.read(buffer,0,buffer.length);  if(temp == -1){   break;  }  fos.write(buffer,0,temp); }  }catch(Exception e){   System.out.println(e); }finally{  try{  fis.close();  fos.close();  }catch(Exception e){   System.out.println(e);  } }}}

byte input stream and byte output stream

Reader FileReaderWriter FileWriter
int read(char[] c, int off, int len)void write(char[] c, int off, int len)
import java.io.*;public class Demo{ public static void main(String args[]){  FileReader fr = null; // 字符输入流  FileWriter fw = null;  try{   fr = new FileReader("e:/from.txt"); // 读取的文件路径   fw = new FileWriter("e:/to.txt");    char[] buffer = new char[100];   int temp = fr.read(buffer,0,buffer.length);   fw.write(buffer,0,temp);  //   for(int i = 0; i<buffer.length; i++){    System.out.println(buffer[i];   }  //  }catch(Exception e){   System.out.println(e);  }finally{   try{    fr.close();    fw.close();   }catch(Exception e){   System.out.println(e);  }  } }}
Decorator mode

BufferedReaderHandles stream character streams input stream, character input processing stream.

MethodreadLine()

import java.io.*;class Test{ public static void main(String args[]){  FileReader fileReader = null;  BufferedReader bufferedReader = null;  tryl{   fileReader = new FileReader("e:/from.txt");   bufferedReader = new BufferedReader(fileReader);   // readLine() 读取一行数据   String line = bufferedReader.readLine();   System.out.println(line);    //     String line = null;    while(true){     line = bufferedReader.readLine();     if(line == null){      break;     }     System.out.println(line);    }  }catch(Exception e){   System.out.println(e);  }finally{   try{    bufferedReader.close();    fileReader.close();  }catch(Exception e){   System.out.println(e);  

Factory mode

interface Student{ public void doWork();}
class Demo1 implements Student{ public void doWork(){  System.out.println("Demo1"); }}
class Demo2 implements Student{ public void doWork(){  System.out.println("Demo2"); }}
class Dashu implements Student{ private Stuent student; public Dashu(Student student){ this.student = student; } public void doWork(){  System.out.println("dashu");  student.doWork(); }}
class School{ public static void main(String[] args){   //    Demo1 demo1 = new Demo1();   Dashu dashu = new Dashu(demo1);   dashu.doWork(); }}

IOFor input and output meaning, read data from the outside InputStream , write data to the outside OutputStream , Stream for the stream, everything in the computer is a binary byte of the world, InputStream and OutputStream to read the binary, we look very troublesome, so there is Reader/Writer the interface, Used to process these character streams.

FileReader fileReader = new FileReader("e:/from.txt");BufferedReader bufferedReader = new BufferedReader(fileReader);String line = bufferedReader.readLine();
try{FileReader fileReader = new FileReader("e:/from.txt");BufferedReader bufferedReader = new BufferedReader(fileReader);String line = null;while(true){ line = bufferedReader.readLine(); if(line == null){  break; } System.out.println(line);}}catch(Exception e){ System.out.println(e);}finally{ try{  bufferedReader.close();  fileReader.close();}catch(Exception e){ System.out.println(e);}

Enhanced API-NIO non-blocking IO

nioNew addition, the role of non-blocking threads.

For the rest of your life, with you.
Jian Suda tert-Niche
A handsome boy, good development habits, ability to think independently, initiative and good at communication
Jane Book Blog: https://www.jianshu.com/u/c785ece603d1

Conclusion
    • I will continue to explain the other knowledge in depth, I am interested to continue to focus on
    • A little gift to walk or like.

Section 40th: IO knowledge case in Java

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.