-1
import java.io.*;
public class ReadFile {
public static void read(File f) throws IOException {
//String delimiters = ".";
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line;
//int numberOfLines = 0;
while ((line = br.readLine()) != null) {
String[] tokens = line.split("\\.", 2);
String p1 = tokens[0];
String p2 = tokens[1];
System.out.println(p1);
System.out.println(p2);
//numberOfLines++;
}
//System.out.println("Numebr of lines in file: " + numberOfLines);
br.close();
fr.close();
}
public static void main(String[] args) {
File f = new File("F:\\Dictionary.txt");
try {
read(f);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
我有一個問題,我在使用一個字典作爲文本文件,我想讀取(字典文件的)行,然後將它分開以便我可以將「單詞」及其「含義」存儲到不同的數組索引中。這String[] tokens = line.split("\\.", 2); to read and split at only the first "." (so that words proceeding after "." will be splitted!). I seem to having an error of ArrayIndexOutOfBound and I don't know why. I want
字符串p1 =令牌[0];存儲單詞和`String p12 =令牌1;這些詞的意思。我該怎麼做? Link for Dictionary。閱讀文件(使用FileReader)和分裂成兩個字符串的Java