简体   繁体   English

如何在java中的输出文本文件中的每一行中打印stringbuilder的内容

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

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. 我正在使用一个很大的excel文件,我必须使用apache事件库对其进行解析。所以现在我能够以某种方式获取每个单元格的值和内容,并将它们存储在Struingbuilder 。现在,我的目标是以这种方式生成文本文件,以便每一行和每一列都看起来像源excel文件。 I am giving the pic of the structure of the excel file. 我给出了excel文件结构的图片。

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 ? 因此,该文件包含24列,所以我可以有一个代码,在Stringbuilder达到24索引后将每个单元格内容放到新行中吗? 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. 就像Stringbuilder包含所有hedaer和数据一样,因此从一开始到Stringbuilder数字达到24时,它将用Stringbuilder将值分成新行。

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. 我不同意您在StringBuilder中进行转储的方法,但是如果您被限制与String builder一起使用,则可以采用这种方法。 Save each each row cell separated by comma and next row separated by "|". 保存每个用逗号分隔的行单元格,并用“ |”分隔下一行。

Then use String tokenizer on "|" 然后在“ |”上使用String标记器 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 现在只需使用StringTokenizer并转储文件中的每一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM