编写程序将一个文件中的所有行逆序存放,其中每行中的所有单词也要逆序,使用ArrayList来完成这个功能。
public class ResortList {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("filetest.txt");
BufferedReader br = new BufferedReader(fr);
ArrayList<String> arr = new ArrayList<>();
String line1;
while((line1=br.readLine())!= null) {
arr.add(line1);
}
for (String s : arr) {
System.out.println(s);
}
ArrayList<String> arr2 = new ArrayList<>();
for (int i = arr.size()-1;i>=0;i--){
arr2.add(arr.get(i));
}
System.out.println("分隔线---------------");
for (String s : arr2) {
System.out.println(s);
}
FileWriter fw = new FileWriter("resort.txt");
BufferedWriter bw = new BufferedWriter(fw);
for (String s : arr2) {
bw.write(s);
bw.newLine();
}
bw.close();
br.close();
}
}