Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:40:01+00:00 2026-06-15T23:40:01+00:00

I am trying to split text files in a directory along a line ‘END

  • 0

I am trying to split text files in a directory along a line ‘END OF CUSTOMER STATEMENT’ and I store the result files into a temporary directory. The split happens only for the first file while the other file is ignored, what is the problem with my code. I was expecting the for loop will engulf all the files in the directory? Here is my code.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 *
 * @author Administrator
 */
public class SplitFiles {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        File f = new File("D:/statements/");
        String[] filenames = f.list();
        File[] texts = f.listFiles();
        String lines = "";
        for (int m = 0; m < filenames.length; m++) {
            try {
                 int count = 0;
                FileInputStream fs = new FileInputStream("D:/statements/" + filenames[m]);
                BufferedReader br = new BufferedReader(new InputStreamReader(fs));

                FileOutputStream fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

                while ((lines = br.readLine()) != null) {
                    String mine = lines.trim();

                    if (mine.startsWith("END OF CUSTOMER STATEMENT")) {
                        bw.close();
                        count++;
                        fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
                        bw = new BufferedWriter(new OutputStreamWriter(fos));
                        continue;
                    }
                    if (mine.isEmpty()) {
                        continue;
                    } else {
                        bw.write(lines);
                        bw.newLine();
                        bw.flush();
                    }
                }
                fos.close();
                fs.close();
                br.close();
                bw.close();
            } catch (Exception ag) {
                System.out.println(ag);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:40:01+00:00Added an answer on June 15, 2026 at 11:40 pm

    I think you should do this in the first place (there are possibly more bugs)

    int count = 0;
    for (int m = 0; m < filenames.length; m++) {
       ...
    

    UPDATE besides, remove your count++ and place it after each file creation

    FileOutputStream fos = new FileOutputStream("D:/DFCU Statements/statement" + count + ".RPT");
    count++;
    

    then it will work as expected

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to split an app.config file into multiple files to make it easier
I am trying to split a large XML file into smaller files using java's
I am trying to load text into memory from different text files. They are
I have a string: [\n['-','some text what\rcontains\nnewlines'],\n\n trying to parse: Regex.Split(@[\n['-','some text what contains
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I am trying to split Scala list like List(1,2,3,4) into pairs (1,2) (2,3) (3,4)
I'm trying to split up a web site into two sections. One which should
I have been running into OutOfMemory Exceptions while trying to load an 800MB text
I have two files that I loaded into Python, and I am trying to
im trying to tokenize text files using the following code: String fileContent = ;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.