我正在做一个项目,需要我将所有信息从 txt 文件复制到数组中。txt 的内容如下所示。我在这里想要的是我想将所有商品名称和描述分别抓取到它的数组中。
GoodTitle Description
Gold The shiny stuff
Wheat What wheaties are made of
Wood To make more ships
Spices To disguise the taste of rotten food
Tobacco Smoko time
Coal To make them steam ships go
Coffee Wakes you up
Tea Calms you down
到目前为止我做了什么:
public void openFile()
{
ArrayList <String> ShippingTokens = new ArrayList<String>();
try{
FileInputStream fstream = new FileInputStream("D://Shipping.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader (new InputStreamReader (fstream));
String strline;
while ((strline = br.readLine()) != null){
strline = strline.trim();
if ((strline.length()!=0)) {
String[] Shippings = strline.split("//s+");
ShippingTokens.add(Shippings[TOKEN_COLUMN]);
}
}
for (String s : ShippingTokens) {
System.out.println(s);
}
in.close();
} catch (Exception e){
System.err.println("Error");
}
}