split text file based on varying row separator
<p>I've one text file. Given below is an example of a file.</p>

<pre><code>CN=100
adshnxhndxghdngfhdsfs
dsjfxnudshxf hfgdsh
ldnf jsdbf hdsbf


CN=200
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe

XN=300
jskhd sa
jd jhgdsxbfgsdhbfgeh
kdsnbdhfhdgfhdbfhd
CN=400
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe

XN=500
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe
</code></pre>

<p>I want to split this file into different files on the basis of row separator. But in my file only = is constant as a row separator. Using etl I've tried using row separator as = and got different files but those were not a expected output and performance issue were there because I had to read files multiple time to get filtered data even though that output didnt match the exact requirement.</p>

<p>I'm not a java developer, but I need small assistance on this. I want my expected out in multiple files from one single file given above like this</p>

<pre><code>File one

CN=100
adshnxhndxghdngfhdsfs
dsjfxnudshxf hfgdsh
ldnf jsdbf hdsbf


File two

CN=200
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe


File three

XN=300
jskhd sa
jd jhgdsxbfgsdhbfgeh
kdsnbdhfhdgfhdbfhd


File four

CN=400
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe


File five

XN=500
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe
</code></pre>

<p>Can I have any suggestion please so that I could perform this activity using java ? Because I think using java all process can be done at one time while reading the file. And then may I can try your suggestion in etl tool as well.</p>

<p>What I have tried:</p>

<p>In etl I was using "=" as a row separator and receiving the split files on the basis of that. So My split files in out comes were like these: </p>

<pre><code>------
File one

CN
---------------
file two

100
adshnxhndxghdngfhdsfs
dsjfxnudshxf hfgdsh
ldnf jsdbf hdsbf


CN
------------------------
file three

200
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe

XN
----------------------
file four

300
jskhd sa
jd jhgdsxbfgsdhbfgeh
kdsnbdhfhdgfhdbfhd
CN
-----------------------
file five

400
jhnxrhewxrgewhgxew
ejxn jehwbf ewhfbew
jdksfn sbfhb hfe

XN
-------------------------------
</code></pre>

<p>I'm recieving kind of these files and again using etl after doing some adding files back to the characters which came into different files. So Its like reading files again and again and even not getting the exact expecting output affecting performance.</p>

<p>This what I figured out from internet to process my file</p>

<pre><code>private String currentFile = "root.txt";

public static final String REGEX = "^\w+=\d+";

public void foo() throws Exception{

  Path path = Paths.get("path/to/your/input/file.txt");
  Files.lines(path).forEach(line -&gt; {
    if(line.matches(REGEX)) {
      //Extract the digit and update currentFile
      currentFile = "File DOC_ID_"+line.substring(3, line.length())+".txt";
      System.out.println("Current file is now : currentFile);
    } else {
      System.out.println("Writing this line to "+currentFile + " :" + line);
      //Files.write(...);
    }
  });
</code></pre>

<p>Please share your inputs and then I will try to take ahead from there.</p>

<p>Thanks in Advance </p>