0

我想在基于序列的列表中将文件拆分为带有详细信息的标题。

想使用 Header 和 detail 拆分文本文件我尝试过这样的事情,但没有帮助。

我想调用迭代器的上一个迭代,但我不能......

文件 :

H>>>>>>

L>>>>>>>

L>>>>>>>

L>>>>>>>

H>>>>>>>

L>>>>>>>

L>>>>>>>

H>>>>>>>

L>>>>>>> ...

我想了 :

清单 1 与 H 、 L 、 L 、 L

清单 2 与 H , L , L

清单 3 与 H , L

代码尝试:

 List<String> poString = new ArrayList<String>();
    if(poString !=null && poString.size() > 0)
            {      
               ListIterator<String> iter = poString.listIterator();
               while(iter.hasNext())
               {
                String tempHead = iter.next();
                List<String> detailLst = new ArrayList<String>();
                if(tempHead.startsWith("H"))
                 {
                    while(iter.hasNext())
                    {
                        String detailt = iter.next();
                        if(!detailt.startsWith("H"))
                          detailLst.add(detailt);
                        else
                        {
                            iter.previousIndex();
                        }
                    }
                 }
               }
4

4 回答 4

0

试试这个,我自己尝试了一点,它有效

    BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    ArrayList<ArrayList<String>> result = new ArrayList<> ();
    int numlines =0;
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            if (line.startsWith("H"))
            { 
                   result.add(new ArrayList<String>());
                   result.get(numlines).add("H");

                   line = br.readLine();

                   while(line != null && !line.startsWith("H")){
                       if(line.startsWith("L")) result.get(numlines).add("L");
                       line = br.readLine();
                   }
                   ++numlines;
            }        
            else line = br.readLine();
        }

    } finally {
        br.close();
    }
于 2013-02-04T13:09:26.697 回答
0

你可以用这个..

public static void main(String a[]) throws Exception
{
    ArrayList<String> headers=new ArrayList();
    ArrayList<String> lines=new ArrayList();
    HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); 
    File f= new File("inputfile.txt");
    Scanner scanner = new Scanner(f);
    try {
      while (scanner.hasNextLine()){
       String ss=scanner.nextLine();
       String key= String.valueOf(ss.charAt(0));
       if ( map.containsKey(key))
       {
           ArrayList<String> temp=(ArrayList) map.get(key);
           temp.add(ss);
           map.put(key, temp);
       }
       else
       {
           ArrayList<String> temp= new ArrayList();
           temp.add(ss);
           map.put(key, temp);
       }
      }
    }
      catch(Exception e)
      {
          throw e;
      }

}

于 2013-02-04T12:41:47.183 回答
0

试试这个(未经测试):

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
        List<StringBuilder> myList = new List<StringBuilder>();
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
        if (line[0] == 'H')
        {
            myList.add(sb);  
            sb = new StringBuilder();                               
        }   
        sb.append(line[0]);        
        line = br.readLine();
    }
} finally {
    br.close();
}
于 2013-02-04T12:35:41.090 回答
0

据我了解,最终H..lines你的文件中有多少List<String>,你想要多少。

如果您不知道确切的数字(在您的示例中为 3),那么您有一个 List 列表 ( List<List<String>>)。

//read the file, omitted

List<List<String>> myList = new ArrayList<<List<String>>();
List<String> lines = null;
boolean createList = false;
while (line != null) {
    if (line.startsWith("H")){
      myList.add(lines);
      lines = new ArrayList<String>();
    }
//if the 1st line of your file not starting with 'H', NPE, you have to handle it
   lines.add(line);
   line=readnextlineSomeHow(); //read next line
}

上面的代码可能无法开箱即用,但它为您提供了思路。

于 2013-02-04T13:00:05.977 回答