IO stream 03 -- video Study Notes for bixiangdong JAVA basic tutorial, 03 -- bixiangdong

Source: Internet
Author: User

IO stream 03 -- video Study Notes for bixiangdong JAVA basic tutorial, 03 -- bixiangdong

Abstract

16 read the conversion stream
17 write a conversion stream
18 stream operation rules-1
19 stream operation rules-2
20. Change the standard input/output device
21 abnormal log information
22. System Information

 

16 read the conversion stream

InputStreamReader in the dense stream system is a bridge between byte streams: It reads bytes and decodes them into characters using the specified charset.

The character set used can be specified by the name or displayed. Otherwise, the default Character Set of the platform will be accepted.

1/* 2 use section 15 in the previous section to read a row of data from the keyboard input and print it in uppercase. The principle of reading a row of data is found. 3 is the readLine method. 4. Can I directly use the readLine method to read a row of data input on the keyboard? 5 6 The readLine method is the method in the BufferedReader class of the bytes stream. 7. The read method entered by the keyboard is the byte stream InputStream method. 8 9. Can I change byte streams to the shard stream and then use the readLine method of the shard stream buffer. 10 11 */12 import java. io. *; 13 public class TransStreamDemo14 {15 public static void main (String [] args) throws IOException16 {17 // obtain the keyboard input object 18 InputStream in = System. in; 19 20 // convert byte streams into bytes streams, and use the converted stream InputStreamReader21 InputStreamReader isr = new InputStreamReader (in); 22 23 // send the string to the buffer to improve efficiency, use BufferedReader24 BufferedReader bufr = new BufferedReader (isr); 25 26 String line = null; 27 while (line = bufr. readLine ( ))! = Null) 28 {29 if ("over". equals (line) 30 break; 31 System. out. println (line. toUpperCase (); 32} 33} 34}

 


17 write a conversion stream

The direct subclass of Writer. OutputStreamWriter is a bridge between the bytes stream and the byte stream. You can use the specified charset to encode the characters written into the stream into bytes.

The character set used can be specified by the name or displayed. Otherwise, the default Character Set of the platform will be accepted.


Source: System. in)
Purpose: To log on to the console (System. out)

1/* write the conversion stream and convert the bytes stream to the byte stream */2 import java. io. *; 3 public class TransStreamDemo2 4 {5 public static void main (String [] args) throws IOException 6 {7 // InputStream in = System. in; 8 // InputStreamReader isr = new InputStreamReader (in); 9 // BufferedReader bufr = new BufferedReader (isr ); 10 // can be simplified to 11 // This is the most commonly used reading keyboard input 12 BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); 13 14 // OutputStream out = System. out; 15 // OutputStreamWriter osw = new OutputStreamWriter (out); 16 // BufferedWriter bufo = new BufferedWriter (osw ); 17 // it can be simplified to 18 BufferedWriter bufo = new BufferedWriter (new OutputStreamWriter (System. out); 19 20 String line = null; 21 while (line = bufr. readLine ())! = Null) 22 {23 if ("over ". equals (line) 24 break; 25 bufo. write (line. toUpperCase (); 26 bufo. newLine (); 27 // refresh 28 bufo. flush (); 29} 30 31} 32}View Code

 

18 stream operation rules-1

Requirement: store the data input by the keyboard into a file.
Source: keyboard
Purpose: File

1 import java. io. *; 2 public class TransStreamDemo3 3 {4 public static void main (String [] args) throws IOException 5 {6 BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); 7 8 BufferedWriter bufw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ("out.txt"); 9 10 String line = null; 11 while (line = bufr. readLine ())! = Null) 12 {13 if ("over ". equals (line) 14 break; 15 bufw. write (line. toUpperCase (); 16 bufw. newLine (); 17 bufw. flush (); 18} 19} 20}View Code

Requirement: You want to print the data of a file on the console.
Source: File
Purpose: To log on to the console

1 import java. io. *; 2 public class TransStreamDemo4 3 {4 public static void main (String [] args) throws IOException 5 {6 BufferedReader bufr = new BufferedReader (new InputStreamReader (new FileInputStream ("ReadIn. java "); 7 BufferedWriter bufw = new BufferedWriter (new OutputStreamWriter (System. out); 8 9 String line = null; 10 while (line = bufr. readLine ())! = Null) 11 {12 if ("over ". equals (line) 13 break; 14 bufw. write (line); 15 bufw. newLine (); 16 bufw. flush (); 17} 18 19} 20}View Code

Basic Rules of stream operations:
The most painful thing is that there are many stream objects and I don't know which one to use.
Three definitions are used to accomplish this:
1. Clarify the source and purpose
Source: input stream, InputStream, Reader
Objective: To output streams, OutputStream, and Writer
2. Check whether the operated data is plain text.
Yes: Batch stream
No: byte stream

3. When the system is clear, the specific object to be used will be clarified.
Devices are differentiated.
Source Device: memory, hard disk, keyboard
Target device: memory, hard disk, console


Example 1: store data in a text file to another text file, that is, copy the file.
Analysis process:
Source: because it is the source, the read stream is used. InputStream, Reader
Is it a text file operation?
Yes! You can select Reader.
In this way, the system is clear.
Next, we need to clearly use the object in this system.
Device: a file on the hard disk
The object that can operate on files in the Reader is FileReader.
Need to improve efficiency? Yes! Add the buffer in the Reader system, BufferedReader
FileReader fr = new FileReader ("a.txt ");
BufferedReader bufr = new BufferedReader (fr );


Purpose: Use the write stream for the purpose. OutputStream, Writer
Is it plain text?
Yes! Writer
Device: hard disk, a file
In the Writer system, the object for file operations is FileWriter.
Need to improve efficiency? Yes! Add the buffer in the Writer system, BufferedWriter

FileWriter fw = new FileWriter ("a.txt ");
BufferedWriter bufw = new BufferedWriter (fw );


19 stream operation rules-2

2. Requirement: Save the data entered by the keyboard to a file.
Both source and target exist in this requirement.
Then, analyze them separately.
Source: InputStream Reader
Is it plain text? Yes! Use Reader.
Device: keyboard, corresponding to System. in
Didn't you select Reader? Does System. in correspond to a byte stream?
For the convenience of operating the keyboard text data, it is most convenient to convert the data into a character stream according to the string.
Therefore, since Reader is clearly defined, System. in is converted to Reader.
The Reader system is used to convert the stream, InputStreamReader.
InputStreamReader isr = new InputStreamReader (System. in );

Need to improve efficiency? Yes! BufferedReader
BufferedReader bufr = new BufferedReader (isr );

Purpose: OutputStream Writer
Is it a plain text file? Yes! Writer
Device: hard disk, a file, using FileWriter
FileWriter fw = new FileWriter ("c.txt ");
Need to improve efficiency? Yes!
BufferedWriter bufw = new BufferedWriter (fw );
_____________________________________________
Extended: You want to save the input data to the file according to the specified encoding table (UTF-8.

Purpose: OutputStream Writer
Is it a plain text file? Yes! Writer
Device: hard disk, a file, using FileWriter

However, you need to add the specified encoding table to the storage, while the encoding table can only be specified by the conversion stream.
Therefore, the object to be used is OutputStreamWriter.
The conversion Stream object must accept a byte output stream that can operate files. FileOutputStream
OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream ("d.txt"), "UTF-8 ");

FileWriter fw = new FileWriter ("c.txt ");
Need to improve efficiency? Yes!
BufferedWriter bufw = new BufferedWriter (fw );

1 import java. io. *; 2 public class TransStreamDemo5 3 {4 public static void main (String [] args) throws IOException 5 {6 BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); 7 BufferedWriter bufw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ("a.txt"), "UTF-8"); 8 9 String line = null; 10 while (line = bufr. readLine ())! = Null) 11 {12 if ("over ". equals (line) 13 break; 14 bufw. write (line); 15 bufw. newLine (); 16 bufw. flush (); 17} 18 19} 20}View Code

Enter "Hello!" on the keyboard !", The file size obtained by using the default GBK encoding is 8 bytes.

Specifies that the UTF-8-encoded file is 11 bytes.

Note:

GBK, full name: Chinese character internal code extension specification, expressed in dubyte in both Chinese and English.

UTF-8: A Multi-byte encoding used to address international characters. It uses 8 bits (one byte) for English and 24 bits (three bits) for Chinese characters. UTF-8 contains all the characters needed by all countries in the world, is an international code, universal.


20. Change the standard input/output device

The System class in the java. lang package contains the setIn and setOut methods, which are used to re-allocate standard input streams and output streams respectively.

1 import java. io. *; 2 public class TransStreamDemo5 3 {4 public static void main (String [] args) throws IOException 5 {6 // reallocation of the standard input stream 7 System. setIn (new FileInputStream ("PersonDemo. java "); 8 9 BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); 10 BufferedWriter bufw = new BufferedWriter (new OutputStreamWriter (System. out); 11 12 String line = null; 13 while (line = bufr. readLine ())! = Null) 14 {15 if ("over ". equals (line) 16 break; 17 bufw. write (line); 18 bufw. newLine (); 19 bufw. flush (); 20} 21 22} 23}View Code

 


21 abnormal log information

Write the thrown exception information to the log file.

1 import java. io. *; 2 import java. util. *; 3 import java. text. *; 4 public class ExceptionInfo 5 {6 public static void main (String [] args) 7 {8 try 9 {10 int [] arr = new int [2]; 11 System. out. println (arr [2]); 12} 13 catch (Exception e) 14 {15 try16 {17 Date d = new Date (); 18 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 19 String s = sdf. format (d); 20 21 PrintStream ps = new PrintStream ("exc Eption. log "); 22 ps. println (s); 23 System. setOut (ps); 24} 25 catch (IOException ie) 26 {27 throw new RuntimeException ("failed to create the exception file! "); 28} 29 e. printStackTrace (System. out); 30} 31} 32}View Code

 

However, this is troublesome to write. On the Internet, there is a Log4j tool dedicated to java exception logs.


22. System Information

Store system information in a file. Mainly used methods:

1. The getProperties method in the System class is used to determine the attributes of the current System.

2. list (PrintStream out) in the Properties class, which outputs the attribute class table to the specified output stream.

1 import java. io. *; 2 import java. util. *; 3 public class SystemInfo 4 {5 public static void main (String [] args) throws IOException 6 {7 Properties prop = System. getProperties (); 8 prop. list (new PrintStream ("sysinfo.txt"); 9} 10}View Code

 

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.