簡體   English   中英

從列表中讀取數據並創建字符串以寫入平面文件

[英]read data from list and create string to write flat file

我在ArrayList中有數據行。 我需要從列表中讀取數據並將其轉換為另一種格式並寫入平面文件。

List Data:
General Details|S|!|!|66T4051|N|MACH|a
Charge Details|S|!|!|66T4051| 3827|N|
Charge Details|S|!|!|66T4051| 3828|N| 
Insurance Details|S|!|!|66T4051|   f|
Insurance Details|S|!|!|66T4051|   h|
Insurance Details|S|!|!|66T4051|   p|

這里GeneralDetails計數為1,這是主數據。 收費明細計數為2。保險明細計數為3。

我需要將以下列表數據轉換為以下格式的一個字符串。

ROW1|General Details|S|!|!|66T4051|N|MACH|a|2|Charge Details|S|!|!|66T4051| 3827|N|Charge Details|S|!|!|66T4051| 3828|N|3|Insurance Details|S|!|!|66T4051|   f|Insurance Details|S|!|!|66T4051|  h|Insurance Details|S|!|!|66T4051|   p$ 

我需要在每條記錄之前放置計數。 可以說。

ROW1 - 1 is the count of General Details
|2|Charge Details - 2 is the count of Charge Details
|3|Insurance Details - 3 is the count of Insurance Details

請提出如何實現這一目標?

我得到每一行的計數。

map>{Charge Details=2, General Details=1,Insurance Details=3}

但之后我無法繼續。

public void processRows(LinkedList<LinkedList<Object>> linkedList,String intfCode,String bankId)
{
for(int i=0;i<linkedList.size();i++)
{
   List<Object> list =  linkedList.get(i);
   //System.out.println("list>>"+i+""+list);

   List<Object> tableNameList =  new ArrayList<Object>();
   LinkedHashMap map = new LinkedHashMap();

   for(int j=0;j<list.size();j++)
   {
       String rowOfLine = (String)list.get(j);
       String split[] = (rowOfLine.split("\\|"));
       String tableName=split[0];
       tableNameList.add(tableName);
   }
   for(int j=0;j<list.size();j++)
   {
       String rowOfLine = (String)list.get(j);
       String split[] = (rowOfLine.split("\\|"));
       String tableName=split[0];

       int count = Collections.frequency(tableNameList, tableName);
       System.out.println("count>>"+count);
       if(!(map.containsKey(tableName)) && tableName!=null)
       {
       map.put(tableName, count);
       }
       //transformRow(tableName,intfCode,bankId,rowOfLine,collateralType);
   }
       System.out.println("map>"+map);


}
}

遍歷所有列表,並注意列表的正確順序。 每當有新的列表類型開始時,在迭代之前將列表數附加到您的String中。

I have rows of data in ArrayList. I need to read the data from List and convert it to another format and write to flat file.

這是一個解決問題的方法。 請記住,我尚未測試此代碼,目的是向您展示如何解決問題的想法。 假設帶有數據的ArrayList命名為contents ...

Map<String, List<String>> indexedContents = new HashMap<String, List<String>>();
for (String str : contents) {
    String[] splittedString = str.split("\\|");
    if (splittedString.length > 0) {
    //Index your map on the first entry which is something like
    //"Charge Details"
    if(indexedContents.get(splittedString[0]) == null){
        indexedContents.put(splittedString[0], new ArrayList<String>());
    }
    indexedContents.get(splittedString[0]).add(str);
    }
}
StringBuffer output = new StringBuffer();
for(String key : indexedContents.keySet()){
    output.append(indexedContents.get(key).size()).append("|");
    for(String values : indexedContents.get(key)){
    output.append(values).append("|");
    }
}

由於是List ,因此地圖中的條目將跟蹤您的尺寸。 最后的for循環實際上會創建您的輸出格式,您可以根據需要更改輸出格式。 我希望這個對你有用。

這是您所需的代碼。

碼:

導入java.util.ArrayList;

public class Sample1 {

    public static void main( String[] str ) {
        ArrayList<String> list = new ArrayList<String>();
        list.add( "General Details|S|!|!|66T4051|N|MACH|a" );
        list.add( "Charge Details|S|!|!|66T4051| 3827|N|" );
        list.add( "Charge Details|S|!|!|66T4051| 3828|N|" );
        list.add( "Insurance Details|S|!|!|66T4051|   f|" );
        list.add( "Insurance Details|S|!|!|66T4051|   h|" );
        list.add( "Insurance Details|S|!|!|66T4051|   p|" );

        StringBuffer mainBuf = new StringBuffer();
        StringBuffer tempBuf = new StringBuffer();

        int count = 0;
        String header = null;
        for( String item : list ) {
            String[] values = item.split( "\\|" );

            // if this is first element
            if( header == null ) {
                header = values[0];
                count = 1;
            }
            // check if the previous element and present element is not same.
            else if(!header.equals(values[0])) {
                append( count, header, mainBuf, tempBuf.toString() );
                tempBuf.delete( 0, tempBuf.length() );
                count = 1;
                header = values[0];
            } else {
                count++;
            }
            if( tempBuf.length() > 0 && tempBuf.charAt( tempBuf.length() - 1 ) != '|' ) {
                tempBuf.append( "|" );
            }
            tempBuf.append( item );
        }
        append( count, header, mainBuf, tempBuf.toString() );

        // replace last character as $ 
        int lastIndex = mainBuf.length();
        mainBuf.replace( lastIndex - 1, lastIndex, "$" );
        System.out.println(" mainBuf => " + mainBuf );
    }

    public static void append( int count, String header, StringBuffer mainBuf, String str ) {
        if( header.equalsIgnoreCase(  "General Details" ) ) {
            mainBuf.append( "ROW" + count + "|" ).append( str );
        } else {
            if( mainBuf.charAt( mainBuf.length() - 1 ) != '|' ) {
                mainBuf.append( "|" );
            }
            mainBuf.append( count + "|" ).append( str );
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM