Invert the contents of a text box using the Java IO stream

Source: Internet
Author: User

Import Java.io.*;import Java.util.arraylist;public class test04 {public    static void Main (String args[]) throws Ioexc eption {        BufferedReader br = new BufferedReader (New FileReader ("Xxx.txt"));        BufferedWriter bw = new BufferedWriter (New FileWriter ("Yyy.txt"));        arraylist<string> list = new arraylist<> ();        String Line;        while (line = Br.readline ())!=null) {            list.add (line);        }        int i;        For (I=list.size () -1;i>=0;i--) {            bw.write (List.get (i));            Bw.newline ();        }        Br.close ();        Bw.close ();    }}

  

Overall idea:

1. Create an input/output stream

2. Create a Collection

3. Read the data into the collection

4. The elements in the collection are traversed backwards.

5, Mathematic

Attention

1. BufferedReader br = new BufferedReader (New FileReader ("Xxx.txt")); This form of notation, Br.read returns a string type. When you define a generic for a collection, you define the string type.

2, for the set, according to the index, the value is List.get (i)

The above code, most of all, is the kind of collection that is adopted! Finally selected collection subset ArrayList, because ArrayList addition and deletion of elements convenient, traversal is also convenient, the key is also the index value.

Invert the contents of a text box using the Java IO stream

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.