java如何把txt里的单词读入二维数组中
求高手解答txt里的内容:
abandon vt.丢弃;放弃,抛弃
ability n.能力;能耐,本领
abnormal a.不正常的;变态的
aboard ad.在船(车)上;上船
abroad ad.(在)国外;到处
absence n.缺席,不在场;缺乏
absent a.不在场的;缺乏的
absolute a.绝对的;纯粹的
absolutely ad.完全地;绝对地
absorb vt.吸收;使专心
abstract a.抽象的 n.摘要
abundant a.丰富的;大量的
abuse vt.滥用;虐待 n.滥用
。。。。。。。。
[解决办法]
package a;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ReadWord { /** * @param args * @author sunstar * @throws FileNotFoundException * @date 2012-7-4 上午11:14:15 */ public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("D:\\word.txt") ; BufferedReader br = null ; //把文本中的每一行读入list List list = new ArrayList() ; String word = null ; if (file.exists()){ try{ br = new BufferedReader(new FileReader("D:\\word.txt")); while((word = br.readLine()) != null){ list.add(word) ; } }catch(Exception e){ e.printStackTrace() ; }finally{ try { br.close() ; } catch (IOException e) { e.printStackTrace(); } } } //根据list行数创建一个字符串数组 String [][] wordArr = new String[list.size()][2] ; String tmp = null ; int idx = 0 ; for (int i = 0; i < list.size(); i++){ tmp = (String)list.get(i) ; tmp = tmp.trim() ; idx = splitIdx(tmp) ; if (idx > 0){ wordArr[i][0] = tmp.substring(0, idx) ; wordArr[i][1] = tmp.substring(idx+1) ; } } for (int i = 0; i < wordArr.length; i++){ System.out.println(wordArr[i][0] + "---" +wordArr[i][1]) ; } } public static int splitIdx(String str){ int idxSpace = str.indexOf(" ") ; return idxSpace ; } }