简体   繁体   中英

how to split 100 lines in a xml file which has n number of lines and add it to sub files using java

I am new to the programming please help me regarding this scenario. I am trying to design a code for a program

I have a xml files which contains X number of lines and i need to put first 100 lines of that file to another sub file and next to another sub file and so on up to the end. naming convention should be like file1, file2,....

Input files will be of 5000, 10000 or even more lines

I need a dynamic code for this scenario using dom parser

i designed a code for a file with constant lines.

import java.io.*;  
public class splitting
{  
public static void main(String args[])throws Exception
{  
     int count = 0;
        BufferedReader br = null;
        FileWriter fileWriter1 = new FileWriter("C:\\senderoutput1.txt");
        FileWriter fileWriter2 = new FileWriter("C:\\senderoutput2.txt");
        FileWriter fileWriter3 = new FileWriter("C:\\senderoutput3.txt");
        FileWriter fileWriter4 = new FileWriter("C:\\senderoutput4.txt");

        try {
            String currentLine;
            br = new BufferedReader(new FileReader("C:\\senderinput.txt"));
            while ((currentLine = br.readLine()) != null) 
            {
                count++;

                if (count <= 100) 
                {

                    fileWriter1.write(currentLine + System.getProperty("line.separator", "\r\n"));

                } else if (count > 100 && count <= 200)
                {
                    fileWriter2.write(currentLine + System.getProperty("line.separator", "\r\n"));
                }else if (count > 200 && count <= 300)
                {
                    fileWriter3.write(currentLine + System.getProperty("line.separator", "\r\n"));
                }else if (count > 300 && count <= 400)
                {
                    fileWriter4.write(currentLine + System.getProperty("line.separator", "\r\n"));
                }
            }
        } finally 
            {
            if (br != null) 
            {
                br.close();
            }
            fileWriter1.close();
            fileWriter2.close();
            fileWriter3.close();
            fileWriter4.close();
            System.out.println("File Splitting was successful!!!");
            }
}  
}

this code is for the file which has 400 lines.

how to do it for n number of lines?

You can do something like this to achieve what you are trying to achieve:

BufferedReader br = null;
FileWriter fileWriter = new FileWriter("C:\\senderoutput1.txt");
try {
    String currentLine = null;
    br = new BufferedReader(new FileReader("C:\\senderinput.txt"));
    while ((currentLine = br.readLine()) != null) {
        /* Increment Counter */
        ++count;
        /* Write Text To File */
        fileWriter.write(currentLine + System.getProperty("line.separator", "\r\n"));
        /* Check Split Condition */
        if (count % 100 == 0) {
            /* Close Already Open File */
            fileWriter.close();
            /* Point To New File */
            fileWriter = new FileWriter("C:\\senderoutput" + (count/100 + 1) + ".txt");
        }
    }
    /* Close Last Open File */
    fileWriter.close();
} finally {
    if (br != null) {
        br.close();
    }
    System.out.println("File Splitting Completed Successfully!!!");
}

Please note that this is just to give you an idea and you can modify it as per your needs.

Beginner approach:

  • 1) Read the next line in a loop.
  • 2) Increment a counter & append the current line into a String.
  • 3) Every time the counter hits a new 100 decimal value, write out the String (which contains the collection of lines) to a new file.
  • 4) Clear the string.

I believe it's much better to make a description, then giving out code, especially for beginners.

Write the n lines in m no. of files (as User input ):

You should try this easiest code to achieve this dynamically.

I am using here Buffered Reader, Buffered Writer and java streams to get this:

//Code here:

        String curpath = System.getProperty("user.dir");

        List<String> list = null;
        BufferedWriter bw1 = null;

        Scanner sc = new Scanner(System.in);
        System.out.println("How many lines you want per file, Please Enter");
        String nfr = sc.nextLine();
        int nfr1 = Integer.parseInt(nfr);

        System.out.println("How many file you want, Please Enter");
        String nfr2 = sc.nextLine();
        int nfr3 = Integer.parseInt(nfr2);
        sc.close();

        int count = 1;
        int i = nfr1; // no. of lines per file
        int b = 0;

        for (int j = 1; j <= nfr3; j++) { // nfr3 -- no. of files create

            BufferedReader br = new BufferedReader(new FileReader(curpath + "//datafile.txt"));

            list = br.lines().skip(b).limit(i).collect(Collectors.toList());

            b = count * i; //skip the lines 

            File file1 = new File(curpath + "//Split_Line" + "//file" + j + ".txt" + " ");
            file1.getParentFile().mkdirs();
            file1.createNewFile();

            bw1 = new BufferedWriter(new FileWriter(file1));

            for (String record : list)
                bw1.write((record + System.lineSeparator()));

            br.close();
            bw1.flush();

            count++;

        }

        System.out.println("Split_line file complete");
        bw1.close();

I suppose, it would be helpful to you. If you want to suggest anything to improve code. please comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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