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

标签 java domparser

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

我有一个 xml 文件,其中包含 X 行,我需要将该文件的前 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!!!");
}

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

关于java - 如何使用java在具有n行的xml文件中拆分100行并将其添加到子文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943400/

相关文章:

java - Xml 文档读取尝试错误

java - 线程和java/服务器-客户端程序

java - 斯坦福 corenlp : Unrecoverable error while loading a tagger model

java - 如何跳过 well-formed for java DOM 解析器

java - 如何从 java 中的 DOM 解析器读取特定的 XML 标签<示例提到>

Java DOM XML 解析如何遍历多个节点级别

java - 奇怪的 JAVA UTF-8 编码行为,新的 String(bytes ,"UTF-8") 在大多数相似的设置上给出不同的结果

java - 在 Android 布局中挖洞

java - 使用 spring-data-jpa 自定义 ItemReader