Java基础知识—File文件操作练习作业—05
5.键盘录入一个文件路径,将该文件进行反转,
第一行变成最后一行,
第二行变成倒数第二行;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
*
* @author Fight_steven
*/
public class BufferedReader02 {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
while(true){
do{
System.out.println("please input file path:");
}while (false);
String path = in.nextLine();
File filepath = new File(path);
if(!filepath.exists()){
System.out.println("Error! input file path try once again:");
continue;
}
FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader(fr);
ArrayList<String> list = new ArrayList<>();
String line ;
while((line = br.readLine()) != null){
list.add(line);
}
Collections.reverse(list);
for(String string:list){
System.out.println(string);
}
break;
}
}
}