JAVA Basics------IO Operations (ii) __java

Source: Internet
Author: User

character Streams

"Write data to a file"

Now we use character streams

   
   
    
    /**    *  character streams    *  write data    * */&N
    
    Bsp import java.io.*;   class hello{      public static void main (String[]& Nbsp;args)  throws ioexception {          string&nbs P;filename= "D:" +file.separator+ "Hello.txt";           file f=new file (fileName);           writer out =new filewriter (f);           string str= "Hello";           out.write (str);           out.close ();
   
          }  } 

When you open hello. TXT, you'll see hello.

In fact, the previous example in this example makes no difference, but you can simply type the string without having to convert the string into a byte array.

When you want to ask for additional content in the file, you can use the same line as the one above to:

Writer out =new FileWriter (f,true);

This way, when you run the program, you find that the contents of the file become:

Hellohello If you want to change lines in a file, you need to use "\ r \ n"

For example, change str to string str= "\r\nhello";

The contents of the STR that are appended to the file are then wrapped.

Read from the file:

/**   & nbsp;*  character streams    *  read content from files    * */  import java.io.* ;   class hello{      public static void main (String[]& Nbsp;args)  throws ioexception {          string&nbs P;filename= "D:" +file.separator+ "Hello.txt";           file f=new file (fileName);           char[] ch=new char[100];   

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.