Java Read and write function

Source: Internet
Author: User

/*
Character Stream:
Reader Writer.

Demand:
Create a file on your hard disk and write some data.
*/
Importjava.io.*;

/*
Creates a FileWriter object, invokes a window resource, and creates a destination for data storage at the specified location.
If the file you want to create already exists under the specified directory, it will be overwritten.

FileWriter FW = new FileWriter ("Demo.txt");

Writes a string to the stream through the stream object write method.
Fw.write ("ABCDE");

Close the resource and clear it first.
Fw.close ();



Requirements: The original file for the continuation of the data write.


1, set the object.
2, use the read-write method.
3, close the resource.

*/

classFilewriterdemo
{
Public Static voidMain (string[] args)//throws IOException
{
FileWriter FW=Null
Try
{
Fw= NewFileWriter ("K:\\demo.txt", true);

Fw.write ("Qqqqq\r\nmmmm");

}
Catch(IOException e)
{
System.out.println (E.tostring ());
}
finally
{
if(FW!=Null
Try
{
Fw.close ();
}
Catch(IOException e)
{
System.out.println ("Close:"+E.tostring ());
}

}

}
}
Package Model;import Java.io.filenotfoundexception;import Java.io.filereader;import java.io.ioexception;public Class Filereaddemo {public static void main (string[] args) {try {FileReader fr = new FileReader ("E:\\demo.txt"); char[] Arr = new Char[1024];int num = 0; try {while (num = Fr.read (arr))!=-1)    {        System.out.println (new String (Arr,0,num));}    } catch (IOException E {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

  

Java Read and write function

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.