Character stream of Java io

Source: Internet
Author: User

  1. Character Stream

In the program a character equals two bytes, Java provides us with reader and writer two classes dedicated to manipulating character streams

1) Character output stream: Writer

Writer is a character stream, which is an abstract class, so to use it, it must also be instantiated by its subclass before it can be used.

common methods of writer class

Method Name

Description

Public abstract void Close () throws IOException

Turn off the output stream

Public void Write (String str) throws IOException

Output a string

Public void Write (char cbuf) throws IOException

Output a character array

Public abstract void Flush () throws IOException

Mandatory empty Cache

Example 1:helloworld

Writing data to a text file through a character output stream

[Java]  View Plain copy print?
  1. public static void Main (string[] args ) throws Exception {
  2.   
  3. //Declare a file object
  4.   
  5. File File = new file ("Hellowolrd.txt");
  6.   
  7.   
  8.   
  9. //Declare a Write Object
  10.   
  11. writer writer = null;
  12.   
  13. //Use the FileWriter class to instantiate an object of the writer class and write it in an appended form
  14.   
  15. writer = new FileWriter (file, true);
  16.   
  17.   
  18.   
  19. //Declare a string to write
  20.   
  21. string str = "Write HelloWorld in string form";
  22.   
  23. //write in a text file
  24.   
  25. writer.write (str);
  26.   
  27. //Refresh
  28.   
  29. Writer.flush ();
  30.   
  31. //close character output stream
  32.   
  33. Writer.close ();
  34.   
  35. }  


2) character input stream: Reader

Reader itself is also an abstract class, and similarly, if you use it, we need to instantiate it by its subclass to use it.

Common methods of reader class

Method Name

Description

Public abstract void Close () throws IOException

public int Read () throws IOException

public int Read (char cbuf) throws IOException

Through the method we see that the reader class only provides a way to read characters

Example 2: or HelloWorld

Read the contents of the text on top of it and display it on the console

[Java]  View Plain copy print?
  1. public static void Main (string[] args) Throws Exception {
  2.   
  3. //Declare a file object
  4.   
  5. File File = new file ("Hellowolrd.txt");
  6.   
  7.    
  8.   
  9. //Declare an object of a reader class
  10.   
  11. Reader reader = null;
  12.   
  13.    
  14.   
  15. //Instantiate reader object by FileReader subclass
  16.   
  17. reader = new filereader (file);
  18.   
  19.    
  20.   
  21. //Declare a character array
  22.   
  23. char[] c = new char[1024x768];
  24.   
  25. //// outputs the content
  26.   
  27. //int len = Reader.read (c);   
  28.   
  29.          
  30.   
  31. //Loop mode one read
  32.   
  33. int len=0;
  34.   
  35. int temp=0;
  36.   
  37. While ((Temp=reader.read ())!=-1) {
  38.   
  39.              
  40.   
  41. c[len]= (char) temp;
  42.   
  43. len++;
  44.   
  45.        }  
  46.   
  47. //Close input stream
  48.   
  49. Reader.close ();
  50.   
  51. //Convert char array to string output
  52.   
  53. System.out.println (new String (c, 0, Len));
  54.   
  55.    
  56.   
  57.     }  


2. The difference between a character stream and a byte stream

The operation of the byte stream operation does not use the buffer itself, it is the direct operation of the file itself, and the byte stream is used to buffer the operation.

If we do not close the stream while manipulating the character stream, the data we write cannot be saved. So you must remember to close the stream when you manipulate the character stream.

Character stream of Java io

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.