Java codepackage 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 ;
}
}