可以说我有一个像这样的文本文件:

ALASKA             30-DEC-11
CD  STATION         ICAO  IATA  SYNOP   LAT     LONG   ELEV   M  N  V  U  A  C

AK ADAK NAS PADK  ADK   70454  51 53N  176 39W    4   X     T          7 US

AK AKHIOK           PAKH  AKK
         56 56N  154 11W   14   X                8 US

AK AMBLER           PAFM  AFM          67 06N  157 51W   88   X                7 US

AK ANAKTUVUK PASS   PAKP  AKP          68 08N  151 44W  642   X                7 US


我只对保存以AK开头的行感兴趣。另外,我需要将某些信息保存到数组中,例如我的STATION名称。

对于第一行,我想将“ ADAK NAS”存储到stationArray中,将“ 51”存储到数组中,与“ 53”,“ N”,“ 176”,“ 39”和“ W”相同。我想对以AK开头的每一行都执行此操作。

我真的很困惑如何去做。我当前与此有关的代码如下:

//process text file
FileInputStream fstream = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;

//add lines that start with "AK" to arraylist
ArrayList list = new ArrayList();
while ((strLine = br.readLine()) != null && strLine.startsWith("AK")) {
    list.add(strLine);
}

Iterator itr;
for (itr=list.iterator(); itr.hasNext(); ) {
    String str = itr.next().toString();
    String [] splitSt =str.split("\\t");
    String junk1 = "\\t";


我从网上撤回了迭代器部分,但不知道如何在此处拆分它,或者不知道如何将各个值放入数组中。非常感谢您能提供的任何帮助。谢谢!

最佳答案

您在找这种东西吗?

List<String> list = new ArrayList<String>();
List<String[]> out = new ArrayList<String[]>();
while ((strLine = br.readLine()) != null && strLine.startsWith("AK"))
{
    list.add(strLine);
}
for(String line : list){
    String[] lineArr = str.split("\\t");

}
for (itr=list.iterator(); itr.hasNext(); )
{
    String delimiter = "\\t";
    String [] splitSt =str.split(delimiter);
    //we don't know how many we're going to find,
    //so we should accumulate the data in a List.
    out.add(splitSt);
}
String[] stationArray = out.toArray(new String[out.size()]);

10-07 12:09