当前位置: 代码迷 >> J2EE >> java实现文件的读取并惠存不同的数组中
  详细解决方案

java实现文件的读取并惠存不同的数组中

热度:37   发布时间:2016-04-17 23:34:17.0
java实现文件的读取并存入不同的数组中
我是设置了一个Button button_char按钮,点击按钮之后的操作
例如:读取很多姓名和号码的.txt文件,然后分别存入两个数组中,求代码!!
------解决思路----------------------
用list吧,数组你提前知道多大吗?不然会越界的!

FileReader fr = null;
BufferedReader br = null;
List<String> nameList = new ArrayList<String>();
List<String> phoneList = new ArrayList<String>();
try {
fr = new FileReader(new File("H:\\test.txt"));
br = new BufferedReader(fr);
String line = null;
String[] strs = null;
while ((line = br.readLine()) != null) {
strs = line.split("\\s+");
nameList.add(strs[0]);
phoneList.add(strs[1]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
  相关解决方案