基于Java中的字符串拆分文本文件

时间:2013-01-15 12:10:24

标签: java arraylist split text-files

如何使用string.endwith("Page 1")分割文本文件并存储在arraylist中?

任何人都可以帮我这样做吗?

我试图这样做。

while (readerSplit.hasNextLine()) {
                String sLineDataSplit = readerSplit.nextLine();
                if (!sLineDataSplit.endsWith("Page    1")) {
                    sbSplitData.append(sLineDataSplit);
                    sbSplitData.append(newline);
                } else {
                    sbSplitData.append(sLineDataSplit);
                    sbSplitData.append(newline);
                    if (sbSplitData.length() > 100) {
                        System.out.println("Inside");
                        stringList.add(i, sbSplitData);
                        System.out.println(sbSplitData);
                        i++;
                        sbSplitData.setLength(0);
                    }
                    System.out.println("Outside");
                    System.out.println(sbSplitData);
                }
            }

以下是我文件中的一些文字。

007                                                     Date  1/04/13           Page    1
   1                                                    Account Number         1000000000
   1                                                    Enclosures
013            Test S.A.(Test)
   1           URB. MARBELLA CALLE AQUILINO DE LA
   1           GUARDIA CON CALLE 47 TORRE BANESCO
   1           PISO 27 Y 28 CIUDAD DE PANAMA
   1           PANAMA
024   As of 1/1/2013, funds in a noninterest-bearing transaction account (including an
   1  IOLTA) will no longer receive unlimited deposit insurance coverage.  They will be
   1  insured to the legal maximum of $250,000 for each ownership category.
   2  ................................ ACCOUNT DETAIL ...............................
   0  ................................ ACCOUNT DETAIL ...............................
   2  CHECKING ACCOUNT FRG AFFL BANK
   0  CHECKING ACCOUNT FRG AFFL BANK             Number of Enclosures                  0
   1  Account Number                1000007904   Statement Dates   1/04/13 thru  1/06/13
   1  Previous Balance               50,226.18   Days in the statement period          3
   1     10 Deposits/Credits        338,888.67   Average Ledger                50,768.40
   1     30 Checks/Debits           338,246.00   Average Collected             50,768.40
   1  Total Fees                           .00
   1  Interest Paid                        .00
   1  Current Balance                50,868.85
   3  Deposits and Additions
   0  Deposits and Additions
   1  Date      Description                                   Amount        Refe
   0  Date      Description                                   Amount        Refe
   1   1/04     WIRE-IN 20130040042300 ATLANTI            300,000.00
   1            OBI:
   1   1/04     WIRE-IN 20130040038800 1/CARVA              4,295.00
   1            OBI:
   1   1/04     WIRE-IN 20130030004900 NEW CHA              1,563.67
   1            OBI:PENSION MES DE DICIEMBRE D
   1   1/04     INTERNAL TRF 20130040011800                13,995.00
007                                                     Date  1/04/13           Page    2
   1                                                    Account Number         1000000000
   1                                                    Enclosures
024   CHECKING ACCOUNT FRG AFFL BANK          1000007904  (Continued)
   2  Checks and Withdrawals
   0  Checks and Withdrawals
   1  Date      Description                                   Amount        Refe
   0  Date      Description                                   Amount        Refe
   1   1/04     Transfer to DDA                           292,000.00-
   1            Acct No.       1000007896-D
   1   1/04     WIRE-IN 20130040024900 CARLOS               5,000.00-
   1            OBI:REINTEGRO NESTMARY
   1   1/04     WIRE-IN 20130040025100 ELIZABE              5,000.00-
   1            OBI:PAGO POR ACTIVIDADES DEPOR
   1   1/04     WIRE-IN 20130040027100 COOPERA              2,622.00-
   1            OBI:PAGO
   1   1/04     WIRE-IN 20130040025900 INGENIA              2,550.00-
   1            OBI:PRESTAMO EFECTIVO
   1   1/04     WIRE-IN 20130040013100 JULIANO              2,500.00-
   1            OBI:
   1   1/04     WIRE-IN 20130040013000 MARIA I              2,089.57-
   1            OBI:
   1   1/04     WIRE-IN 20130040013300 JESUS A              2,012.00-
   1   1/04     WIRE-IN 20130040018000 JOSE FR                200.00-
   1            OBI:
   1   1/04     WIRE-IN 20130040027800 EVELYN                 100.00-
   1            OBI:
   2  Daily Balance Information
   0  Daily Balance Information
   1  Date          Balance
   0  Date          Balance
   1   1/04            50,868.85
   2  For more information about FDIC insurance coverage, visit
   1  http://www.fdic.gov/deposit/deposits/unlimited/expiration.html
007                                                     Date  1/04/13           Page    1
   1                                                    Account Number         1000000000
   1                                                    Enclosures                     25
013            Test
   1           7A AVENIDA 11-59 ZONA 9
   1           EDIFICIO GALERIAS ESPAÑA
   1           GUATEMALA CITY
   1           GUATEMALA
024   As of 1/1/2013, funds in a noninterest-bearing transaction account (including an
   1  IOLTA) will no longer receive unlimited deposit insurance coverage.  They will be
   1  insured to the legal maximum of $250,000 for each ownership category.
   2  ................................ ACCOUNT DETAIL ...............................
   0  ................................ ACCOUNT DETAIL ...............................
   2  FINANCIAL INSTITUTIONS
   0  FINANCIAL INSTITUTIONS                     Number of Enclosures                 25
   1  Account Number                1000059038   Statement Dates   1/01/13 thru  1/06/13
   1  Previous Balance            2,290,044.74   Days in the statement period          6
   1      7 Deposits/Credits        952,853.71   Average Ledger             1,836,411.23
   1     33 Checks/Debits         1,522,979.73   Average Collected          1,836,411.23
   1  Total Fees                           .00
   1  Interest Paid                        .00
   1  Current Balance             1,719,918.72
   3  Deposits and Additions
   0  Deposits and Additions
   1  Date      Description                                   Amount        Refe
   0  Date      Description                                   Amount        Refe
   1   1/02     WIRE-IN 20130020001800 ONMOBIL              2,500.00
   1            OBI:FOR THE FINAL CREDIT OF AN
   1   1/02     WIRE-IN 20130020009400 VIGO RE                114.00
   1            OBI:VIGO SETTLEMENT GTV14
   1   1/03     WIRE-IN 20130030030100 BANCO I            800,000.00
   1            OBI:TRASLADO DE FONDOS DE SU C
   1   1/03     WIRE-IN 20130030013700 MR BEYN              1,324.90
   1            OBI:C/C 17026009260 ANA ANGELI
   1   1/03     Remote Deposit Capture Deposit             70,725.00
   1   1/04     WIRE-IN 20130040010500 VIGO RE             10,826.73
   1            OBI:VIGO SETTLEMENT GTV14
   1   1/04     Remote Deposit Capture Deposit             67,363.08
   2  Checks and Withdrawals
   0  Checks and Withdrawals
   1  Date      Description                                   Amount        Refe
   0  Date      Description                                   Amount        Refe
   1   1/02     WIRE-IN 20130020023200 SAN LAZ            394,020.00-
   1            OBI:/RFB/ F/F/C TO ACCOUNT 569
   1   1/02     WIRE-IN 20130020029300 UNISOUR             22,589.50-
   1            OBI:/RFB/UNIVERSITY EXPENSES S
   1   1/02     WIRE-IN 20130020029700 UNISOUR             10,020.00-
   1            OBI:/RFB/DEPOSIT TO ACCOUNT
   1   1/02       7367*               17,000.00  1/03       7397                15,000.00
   1   1/04       7370*              100,000.00  1/04       7403*               20,000.00
   1   1/03       7371                20,000.00
   1  * Denotes missing check numbers
   2  Daily Balance Information
   0  Daily Balance Information
   1  Date          Balance
   0  Date          Balance       Date          Balance
   0                              Date          Balance
   1   1/01         2,290,044.74   1/03         1,816,672.91
   1   1/02         1,751,993.61   1/04         1,719,918.72
   2  For more information about FDIC insurance coverage, visit
   1  http://www.fdic.gov/deposit/deposits/unlimited/expiration.html
007                                                     Date  1/04/13           Page    1
   1                                                    Account Number         1000000000
   1                                                    Enclosures
013            Test
   1           (SETTELMENT VISA ACCOUNT)
   1           7A AVENIDA 11-59 ZONA 9
   1           EDIFICIO GALERIAS ESPAÑA
   1           GUATEMALA CITY
   1           GUATEMALA
024   As of 1/1/2013, funds in a noninterest-bearing transaction account (including an
   1  IOLTA) will no longer receive unlimited deposit insurance coverage.  They will be
   1  insured to the legal maximum of $250,000 for each ownership category.
   2  ................................ ACCOUNT DETAIL ...............................
   0  ................................ ACCOUNT DETAIL ...............................
   2  FINANCIAL INSTITUTIONS
   0  FINANCIAL INSTITUTIONS                     Number of Enclosures                  0
   1  Account Number                1000062412   Statement Dates   1/01/13 thru  1/06/13
   1  Previous Balance              385,002.47   Days in the statement period          6
   1        Deposits/Credits               .00   Average Ledger               374,620.47
   1      7 Checks/Debits            15,367.65   Average Collected            374,620.47
   1  Total Fees                           .00
   1  Interest Paid                        .00
   1  Current Balance               369,634.82
   3  Checks and Withdrawals
   0  Checks and Withdrawals
   1  Date      Description                                   Amount        Refe
   0  Date      Description                                   Amount        Refe
   1   1/02     WIRE-OUT 20130020006601                     6,293.81-
   1            OBI:4423210000 VSS SETTLEMENT
   1   1/02     WIRE Out Fee                                   20.00-
   1   1/03     WIRE-OUT 20130030011401                     3,541.38-
   1            OBI:4423210000 VSS SETTLEMENT
   1   1/03     WIRE Out Fee                                   20.00-
   1   1/04     Account Service Fee Corresp.Bk                 80.00-
   1   1/04     WIRE-OUT 20130040010401                     5,392.46-
   1            OBI:4423210000 VSS SETTLEMENT
   1   1/04     WIRE Out Fee                                   20.00-
   2  Daily Balance Information
   0  Daily Balance Information
   1  Date          Balance
   0  Date          Balance       Date          Balance
   0                              Date          Balance
   1   1/01           385,002.47   1/03           375,127.28
   1   1/02           378,688.66   1/04           369,634.82
   2  For more information about FDIC insurance coverage, visit
   1  http://www.fdic.gov/deposit/deposits/unlimited/expiration.html

等待任何建议或帮助。 提前谢谢......

1 个答案:

答案 0 :(得分:0)

  1. 简单的基本思想是创建一个ArrayList。
  2. 如果newLine方法不包含"Page 1",请创建一个StringBuffer添加字符串。
  3. 如果newLine方法返回的字符串包含"Page 1",则将StringBuffer添加到arraylist中并创建新的StringBuffer。并再次执行相同的步骤。
  4. 对于某些建议:lastIndexOf,您也可以尝试使用contains方法。

    List<StringBuffer> list = new ArrayList<StringBuffer>();
    StringBuffer sb = new StringBuffer();
    int i = 1; // I think page no is changing form 1 to ...
    while (readerSplit.hasNextLine()) {
       String sLineDataSplit = readerSplit.nextLine().trim();
       // take a look for lastIndexOf function.
       if (sLineDataSplit.lastIndexOf("Page    "+i)==9 && sb.length()>0) {
           list.add(sb);
           sb = new StringBuffer();
           sb.append(sLineDataSplit);
           i++; // to get the next page 
        } 
        else{
           sb.append(sLineDataSplit);
        }
      }