繁体   English   中英

如何在具有 n 行的 xml 文件中拆分 100 行并使用 java 将其添加到子文件

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

我是编程新手,请帮助我了解这种情况。 我正在尝试为程序设计代码

我有一个包含 X 行的 xml 文件,我需要将该文件的前 100 行放到另一个子文件中,然后放在另一个子文件旁边,依此类推直到最后。 命名约定应类似于 file1、file2、....

输入文件将是 5000、10000 甚至更多行

我需要使用 dom 解析器的这种情况的动态代码

我为具有固定行的文件设计了一个代码。

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!!!");
            }
}  
}

此代码适用于包含 400 行的文件。

如何为 n 行执行此操作?

您可以执行以下操作来实现您要实现的目标:

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!!!");
}

请注意,这只是为了给您一个想法,您可以根据需要对其进行修改。

初学者方法:

  • 1)循环读取下一行。
  • 2)增加一个计数器并将当前行附加到字符串中。
  • 3)每次计数器达到新的100十进制值时,将String(包含行集合)写到新文件中。
  • 4)清除字符串。

我认为进行描述然后给出代码要好得多,特别是对于初学者。

n行写在m号中。 文件(作为用户输入):

您应该尝试这个最简单的代码来动态地实现这一点。

我在这里使用 Buffered Reader、Buffered Writer 和 java 流来获取:

//这里的代码:

        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();

我想,这会对你有所帮助。 如果您想提出任何改进代码的建议。 请评论

暂无
暂无

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

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