键盘录入一个文件路径,将该文件反转
反转:第一行变成最后一行,第二行变成倒数第二行
package day19;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class HomeWork2 {
public static void main(String[] args) throws Exception {
File f=getFile();
BufferedReader br=new BufferedReader(new FileReader(f));
ArrayList<String>list = new ArrayList();
String line=null;
while((line=br.readLine())!=null) {
list.add(line);
}
br.close();
Collections.reverse(list);
BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\abc.reverse.txt"));
// for(int i=0;i<list.size();i++) {
// bw.write(list.get(i));
// bw.newLine();
// }
for (String str : list) {
bw.write(str);
bw.newLine();
}
bw.flush();
bw.close();
}
private static File getFile() {
Scanner sc = new Scanner(System.in);
while(true) {
String str=sc.nextLine();
File f=new File(str);
if(!(f.isFile())) {
System.out.println("请重新输入");
}else {
return f;
}
}
}
}