日期:2014-05-20 浏览次数:20928 次
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileReaderTest {
static List<String> logs = new ArrayList<String>();
public static List<String> readFile(File f) throws IOException {
if (f == null
------解决方案--------------------
!f.exists()) {
return null;
}
List<String> list = new ArrayList<String>();
String encoding = "UTF-8";
InputStreamReader read = new InputStreamReader(new FileInputStream(f),
encoding);
BufferedReader br = new BufferedReader(read);
String line = null;
while ((line = br.readLine()) != null) {
if(line.startsWith("机器码")){
logs.add(line);
}
list.add(line);
}
br.close();
read.close();
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileReaderTest.readFile(new File("c:/temp/test.txt"));
for(String s : FileReaderTest.logs){
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}