java读取txt文件数据

我想出TXT文件中提取 学生的成绩
例如
张三 语文 80
应该怎么提出来

希望可以帮你。

//读取下载下来的版本文件
public static String readVersion(){
String version = null;
try {
String line = null;
String path = savePath+ "version.txt";
byte[] item = null;
int i = 0;
BufferedReader reader = new BufferedReader(new FileReader(path));
while ((line = reader.readLine()) != null) {
item = line.getBytes();
for (byte a : item) {
i++;
if(a == 61){
version = line.substring(i, (item.length-1));
break;
}
}
}
reader.close();

} catch (Exception e) {
e.printStackTrace();
}
return version;
}

其中这个version就是我要读出来的String。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-11
File file = new File(文件路径);
BufferedReader reader = null;
String[][] score = new String[3][];
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int i = 0;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 假设数据用tab分割
score[0][i]=tempString.split("\\t")[0];//姓名
score[1][i]=tempString.split("\\t")[1];//学科
score[2][i]=tempString.split("\\t")[2];//成绩
i++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
第2个回答  2012-06-11
/**
* 读取文本文件内容
* @param filePathAndName 带有完整绝对路径的文件名
* @return 返回文本文件的内容
* @param encoding 文本文件打开的编码方式
*/
public static ArrayList readTxtToArrayList(String filePathAndName, String encoding)
throws IOException {
System.out.println(filePathAndName);
encoding = encoding.trim();
StringBuffer str = new StringBuffer("");
ArrayList st = new ArrayList();
try {
FileInputStream fs = new FileInputStream(filePathAndName);
InputStreamReader isr;
if (encoding.equals("")) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding);
}
BufferedReader br = new BufferedReader(isr);
try {
String data = "";
while ((data = br.readLine()) != null) {
st.add(data);
data = data.replace(data, "\r\n");
str.append(data + " ");
}
} catch (Exception e) {
str.append(e.toString());
}
} catch (IOException es) {
st = null;
}
return st;
}
第3个回答  2012-06-11
如果换行正确有规律的话就通过读取txt文件,得到每一行循环,在循环的同时,判断一行中是否存在有“张三”和“语文”,且“张三”在“语文”前面,如果符合条件就得到语文后面的成绩!
第4个回答  2012-06-11
如果是想提取出来 最好的方法是用此文件是用java写进取得 是一些乱码
相似回答