How to print the contents of stringbuilder in each line in a output text file in java

拜拜、爱过 提交于 2019-12-11 16:57:13

问题


I am working with an excel file which is a large one and i have to parse it using apache event library.So now i am somehow able to get the each cell value and contents of each cell and i am storing them in a Struingbuilder.So now my aim is to generate the text file in such a way so that each row and column will be look like the source excel file. I am giving the pic of the structure of the excel file.

XXXXXX  XXXXXXXXXXXX  XXXXXXXX  XXXXXXXXXXX  XXXXXXXXX
1         DDD          FFFFFF   KKKK            LLLL
2          KKK          FFFFF   KKK             LLLL
.............................
9          KKK          HHH     LL                JJJ

So i am getting the file content as

public void endElement(String uri, String localName, String name)
            throws SAXException {
        StringBuilder row = new StringBuilder();
        // Process the last contents as required.
        // Do now, as characters() may be called more than once
        if(nextIsString) {
            int idx = Integer.parseInt(lastContents);

            lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
            nextIsString = false;
        }

        // v => contents of a cell
        // Output after we've seen the string contents
        if(name.equals("v")) {
            //System.out.println(lastContents);

            if(!lastContents.isEmpty() )
            row.append(lastContents);

            /*if(lastContents == null) {
                row.append("");
            }else {
                row.append(SEPARATOR);
            }*/

            aMethodToParseRow(row);
            try {
                FileUtils.write(new File("D:\\BM_Amortization_.txt"), formatListToString(pickUpExcelValues));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private void aMethodToParseRow(StringBuilder row) {
        StringBuilder sb =  new StringBuilder();
        StringBuilder sb1 =  new StringBuilder();int count =0;
        if(!(row.toString().equals("Loan details") || row.toString().equals("Fixed") || row.toString().equals("3m")|| row.toString().equals("ACT/364")||row.toString().equals("Amounts * EUR 1")||row.toString().equals("Floating") ||row.toString().equals("ACT/365")||row.toString().equals("43100")||row.toString().toString().equals("6m")||row.toString().toString().equals("ACT/ACT")||row.toString().equals("General information")||row.toString().equals("FA - Reporting")||row.toString().equals("Solvency II Reporting")||row.toString().equals("1y")||row.toString().equals("30/360") )) {
            count++;
            if(count>0 && count<24)
            sb.append(row.toString());
            System.out.println(sb.toString());
        }
        count=0;
    }

So the file contains 24 columns , so can i have a code which will place every cell content to the new line after it comes to 24 index in the Stringbuilder? Like the Stringbuilder contains all the hedaers and the data so starting from the begining when it will reach to 24 number in the Stringbuilder it will break the values into a new line with a separtor.


回答1:


I don't agree with your approach of dumping in StringBuilder but if you are constrained to use with String builder then you can follow this approach. Save each each row cell separated by comma and next row separated by "|".

Then use String tokenizer on "|" so you have your individual rows and then in the loop use tokenizer again on "," to get the cell values. Ex

StringBuilder sample = new StringBuilder();
sample.append("DDD,FFFFFF,KKKK,LLLL).append("|")
       .append("KKK,FFFFF,KKK,LLLL");

Now just use StringTokenizer and dump each row in the file



来源:https://stackoverflow.com/questions/48810857/how-to-print-the-contents-of-stringbuilder-in-each-line-in-a-output-text-file-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!