import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
/**
* 从文件中读取信息
*/
public class ReadFile {
/**
* @param args
* @throws URISyntaxException
*/
public String[] readFile(String fileName){
String[] newStr = null;
FileReader fr = null;
char data[]=new char[1024]; // 建立可容纳1024个字符的数组
String path = null;
try {
path = ReadFile.class.getClassLoader().getResource("").toURI().getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
File f=new File(path + fileName) ;
fr = new FileReader(f);// 建立对象fr
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int num = 0;
try {
num = fr.read(data);// 将数据读入字符列表data内
String str=new String(data,0,num); // 将字符列表转换成字符串
newStr = str.split("\r\n") ;
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return newStr;
}
}