The flow object selection rules in java programming are explained in detail

  • 2020-05-30 20:03:14
  • OfStack

Examples are as follows:


import java.io.*;
public class TransStreamDemo2 {
/**
*  Basic rules of flow operation  * 
 1 , 
 *  Source, keyboard entry 
 *  Purpose. The console 
 * 2 , 
 *  Requirements: want to store keyboard input data to 1 In a file. 
 *  Source: the keyboard 
 *  Purpose: document ( FileoutputStream Can manipulate files) 
 * 3 , 
 *  Need: want to take 1 The data of the file is printed to the console 
 *  Source: a file 
 *  Purpose: console 
 * 
 * 
 *  Basic rules of flow operation 
 *  The most painful thing is that many stream objects do not know which to use 
 * 
 *  This is done with two unambiguities 
 * 1 , source and destination. 
 * 
 Source: input stream, InputStream Reader
 * 
 Purpose: output stream  OutputStream Writer
 * 2 , whether the data of operation is plain text 
 *  is : Characters of the flow  Reader Writer
 *  No: byte stream  InputStreamReader  OutputStreamWriter
 * 3 , when the system is clear then clear to use that specific object 
 * 
 Distinguish by device 
 * 
 Source device: hard disk   memory   The keyboard 
 * 
 Destination device: hard disk    memory   The console 
 * 
 * 
 * 1 Will, 1 Save the data in the file to another 1 In a file. Copy the file 
 * 
 Source: a read stream is used because it is a source. InputStream Reader
 * 
 Do not operate text file: yes    choose   Reader
 *  So the system is clear 
 *  The next step is to specify which objects the system will use 
 *  Clear device: hard disk   On the 1 A file 
 * Reader The objects in the system that can manipulate files are   FileReader
 * 
 * 
 *  Objective: OutputStreamWriter  Whether plain text or not 
 *  Is this: Writer
 *  Equipment: hard disk 1 A file 
 * Writer The objects in the system that can manipulate files are FileReader
 * 
 * FileReader fr=new FileReader("a.txt");
 *  If efficiency is improved: BufferedReader bufr=new BufferedReader(fr);
 * 
 * 
FilrWriter fw=new FileWriter("b.txt");
 *  Whether to improve efficiency: BufferedWriter fw=new BufferedWriter(fw);
 * 
 * */

public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
 BufferedReader bufr=new BufferedReader(new InputStreamReader(new FileInputStream("d://wdl.txt")));
//
 BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d://wdl.txt")));
 BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(System.out));
 
 String line=null;
 
 while((line=bufr.readLine())!=null){
 if("over".equals(line))
 break;
 bufw.write(line.toUpperCase());
 bufw.newLine();
//
 System.out.println(line.toUpperCase());
 bufw.flush();
 }
 bufr.close();
 bufw.close();
}

}

Related articles: