public static void main(String[] args) throws IOException {
//改写后是尽量晚开早关
// 1,创建输入输出流对象
BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));
//2,创建集合对象
ArrayList<String> list = new ArrayList<>();
//3,将读到的数据存储在集合中
String line;
while((line = br.readLine()) != null) {
list.add(line);
}
br.close(); //关流
//4,倒着遍历集合将数据写到文件上
BufferedWriter bw = new BufferedWriter(new FileWriter("revzzz.txt"));
for(int i = list.size() - 1; i >= 0; i--) {
bw.write(list.get(i));
bw.newLine();
}
//5,关流
bw.close();
}
}
|
|