Java Fundamentals Hardening IO flow notes 45:io stream exercises data stored in a collection into a text file case

Source: Internet
Author: User

1. Store the data in the collection into a text file case:

Requirement: To store string data in the ArrayList collection into a text file?

(1) Analysis:
Through the meaning of the topic, we can know some of the following things,
A string is stored in the ArrayList collection.
Traverse the ArrayList collection to get the data.
It is then stored in a text file.
The text file description uses a character stream .

(2)

Data Source :

Arraylist<string>--Traversal to get each string of data

Destination :

A.txt--FileWriter--BufferedWriter

2. code example:

1  Packagecn.itcast_02;2 3 ImportJava.io.BufferedWriter;4 ImportJava.io.FileWriter;5 Importjava.io.IOException;6 Importjava.util.ArrayList;7 8 /*9  * Ten * Data Source: One * arraylist<string>--traverse to get each string data A * Destination: - * A.txt --FileWriter--BufferedWriter -  */ the  Public classArraylisttofiledemo { -      Public Static voidMain (string[] args)throwsIOException { -         //encapsulating a data source (creating a Collection Object) -arraylist<string> array =NewArraylist<string>(); +Array.add ("Hello"); -Array.add ("World"); +Array.add ("Java"); A  at         //Package Destination -BufferedWriter BW =NewBufferedWriter (NewFileWriter ("A.txt")); -  -         //iterating through the collection -          for(String s:array) { -             //Write Data in Bw.write (s); - bw.newline (); to Bw.flush (); +         } -  the         //Freeing Resources * bw.close (); $     }Panax Notoginseng}

Run the effect as follows:

Java Fundamentals Hardening IO flow notes 45:io stream exercises data stored in a collection into a text file case

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.