C. Simply read and write data to a txt file instance

Source: Internet
Author: User

This article describes how to read and write data from C to a txt file instance. If you need it, refer to the example.

The Code is as follows: Copy code
Public void writeData2Txt (String content ){
// Directory for storing files
String fileDir = ServletActionContext. getServletContext (). getRealPath ("D:/file /");
// Generate a simple file name
String fileName = new SimpleDateFormat ("yyyyMMddHHmmsszzz"). format (new Date () + ". txt ";
File txtFile = new File (fileDir, fileName );
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
Try {
BufferedReader = new BufferedReader (new StringReader (content ));
BufferedWriter = new BufferedWriter (new FileWriter (txtFile ));
Char buf [] = new char [1024]; // Character Buffer
Int len;
While (len = bufferedReader. read (buf ))! =-1 ){
BufferedWriter. write (buf, 0, len );
}

} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
BufferedWriter. flush ();
BufferedWriter. close ();
BufferedReader. close ();

} Catch (IOException e ){
E. printStackTrace ();
}
}


}

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.