[英]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.我有一个包含 X 行的 xml 文件,我需要将该文件的前 100 行放到另一个子文件中,然后放在另一个子文件旁边,依此类推直到最后。 naming convention should be like file1, file2,....命名约定应类似于 file1、file2、....
Input files will be of 5000, 10000 or even more lines输入文件将是 5000、10000 甚至更多行
I need a dynamic code for this scenario using dom parser我需要使用 dom 解析器的这种情况的动态代码
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.此代码适用于包含 400 行的文件。
how to do it for n number of lines?如何为 n 行执行此操作?
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: 初学者方法:
I believe it's much better to make a description, then giving out code, especially for beginners. 我认为进行描述然后给出代码要好得多,特别是对于初学者。
Write the n lines in m no.将n行写在m号中。 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:我在这里使用 Buffered Reader、Buffered Writer 和 java 流来获取:
//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请评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.