将文件拆分为多个文件 [英] Split file into multiple files
问题描述
我想剪切一个文本文件。
我想将文件50行减50行。
I want to cut a text file. I want to cut the file 50 lines by 50 lines.
例如,如果文件是1010行,我会恢复21个文件。
For example, If the file is 1010 lines, I would recover 21 files.
我知道如何计算文件数量,行数,但是一写,就行不通。
I know how to count the number of files, the number of lines but as soon as I write, it's doesn't work.
我使用Camel Simple(Talend),但它是Java代码。
I use the Camel Simple (Talend) but it's Java code.
private void ExtractOrderFromBAC02(ProducerTemplate producerTemplate, InputStream content, String endpoint, String fileName, HashMap<String, Object> headers){
ArrayList<String> list = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(content));
String line;
long numSplits = 50;
int sourcesize=0;
int nof=0;
int number = 800;
try {
while((line = br.readLine()) != null){
sourcesize++;
list.add(line);
}
System.out.println("Lines in the file: " + sourcesize);
double numberFiles = (sourcesize/numSplits);
int numberFiles1=(int)numberFiles;
if(sourcesize<=50) {
nof=1;
}
else {
nof=numberFiles1+1;
}
System.out.println("No. of files to be generated :"+nof);
for (int j=1;j<=nof;j++) {
number++;
String Filename = ""+ number;
System.out.println(Filename);
StringBuilder builder = new StringBuilder();
for (String value : list) {
builder.append("/n"+value);
}
producerTemplate.sendBodyAndHeader(endpoint, builder.toString(), "CamelFileName",Filename);
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
if(br != null)br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
不知道Camel的人,这一行用于发送文件:
For people who don't know Camel, this line is used to send the file:
producerTemplate.sendBodyAndHeader (endpoint, line.toString (), "CamelFileName" Filename);
endpoint ==>目的地(其他代码没问题)
endpoint ==> Destination (it's ok with another code)
line.toString()==>值
line.toString () ==> Values
然后是文件名(可以使用其他代码)
And then the file name (it's ok with another code)
推荐答案
你先计算行数
while((line = br.readLine()) != null){
sourcesize++; }
然后你就在文件的末尾:你什么都不读
and then you're at the end of the file: you read nothing
for (int i=1;i<=numSplits;i++) {
while((line = br.readLine()) != null){
在重新阅读之前,你必须回到文件的开头。
You have to seek back to the start of the file before reading again.
但这是浪费时间和电源,因为你将读取文件两次
But that's a waste of time & power because you'll read the file twice
最好一次性读取文件,将其放入列表< String>
(可调整大小),并使用存储在内存中的行继续进行拆分。
It's better to read the file once and for all, put it in a List<String>
(resizable), and proceed with your split using the lines stored in memory.
编辑:似乎你遵循了我的建议并偶然发现了下一个问题。你可能会问另一个问题,好吧......这会创建一个包含所有行的缓冲区。
seems that you followed my advice and stumbled on the next issue. You should have maybe asked another question, well... this creates a buffer with all the lines.
for (String value : list) {
builder.append("/n"+value);
}
您必须使用列表中的索引来构建小文件。
You have to use indexes on the list to build small files.
for (int k=0;k<numSplits;k++) {
builder.append("/n"+list[current_line++]);
current_line
成为您的全球行计数器文件。这样你每次都可以创建50行不同的文件:)
current_line
being the global line counter in your file. That way you create files of 50 different lines each time :)
这篇关于将文件拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!