IO操作_文件读取

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileTool {


public static List readFile2List(String filePath){
List<String> fileContentList = new ArrayList<String>();
BufferedReader buf = null;
try {
buf = new BufferedReader(new FileReader(filePath));

while(buf.ready()){
fileContentList.add(buf.readLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fileContentList;
}

private static void printStrList(List list){
if(list!=null){
for(int i=0;i<list.size();i++){
System.out.println((String)list.get(i));
}
}

}
public static void main(String[] args){
String path = "E:/switchLog/release.txt";
List contentList = readFile2List(path);
printStrList(contentList);

}
}

你可能感兴趣的:(文件读取)