[JAVA]如果从文本文档中读取内容存放到数组中?

2024-11-18 10:58:01
推荐回答(1个)
回答1:

直接在main函数中写了:
(经测试,已经实现)

public static void main(String[] args) {

//自己修改文件存放路径
String filepath = "e:/test.txt";
String temp = null;
Object a[][] = null;
BufferedReader br = null;
StringTokenizer st = null;
int i = 0;
try {
br = new BufferedReader(new FileReader(filepath));
while (br.ready()) {

temp = br.readLine();

if (temp != null && temp != "") {
//个人感觉方法split不怎么好用,还是用以下方法比较好

st = new StringTokenizer(temp, "--");

a[i][0] = st.nextToken().trim();

a[i][1] = st.nextToken().trim();

a[i][2] = st.nextToken().trim();

}
i++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {

br.close();
} catch (Exception e) {

e.printStackTrace();
}
}
}

要查看结果的话遍历一下数组a[][]就可以了