将文件拆分为多个文件 [英] Split file into multiple files
问题描述
我想剪切一个文本文件.我想将文件切成 50 行 x 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 ==> Destination(其他代码也可以)
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
最好一次性读取文件,将其放入List
(可调整大小),然后使用存储在内存中的行进行拆分.
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屋!