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