想要代码,自己也知道用io,可是不太会呀!姓名和号码用空格区分

解决方案 »

  1.   

    用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();
    }
    }
    }