编写一个程序,讲一个目录及其子目录下的所有的txt类型的文本文件内容
合并到若干个新的文本文件中,当第一个新产生的文件中存储的内容达到1M
bytes时,剩下的内容存储到第二个新的文件中,依次往下,
新产生的文本文件知名字依次为1.txt、2.txt。。。。
不知道这个问题该怎么处理,大概的思路有,但是不知道具体该怎么实现,
------解决方案--------------------
抽时间给楼主写了一个demo,给楼主一个编程思路。
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 自定义功能的Writer , 可以随着写入文件的大小切换新文件进行输出。
*/
public class ShiftWriter extends Writer{
public static final long DEFAUT_MAX_SIZE = 1*1024*1024;
public static final String DEFAULT_CHARSET = "GBK";
/** 可计数的输出流 */
static class CounterOutputStream extends OutputStream{
private OutputStream output;
private long count=0;
public CounterOutputStream(OutputStream output) {
this.output = output;
}
@Override
public void write(int b) throws IOException {
output.write(b);
count++;
}
public long getCount() {
return count;
}
}
private long maxSize;
private File targetDir;
private int fileIndex = 1;
private String charset;
public ShiftWriter(File targetDir) throws FileNotFoundException, UnsupportedEncodingException {
this(targetDir,DEFAUT_MAX_SIZE,DEFAULT_CHARSET);
}
public ShiftWriter(File targetDir, String charset) throws FileNotFoundException, UnsupportedEncodingException {
this(targetDir,DEFAUT_MAX_SIZE,charset);
}
/**
* 构造器函数,向指定的文件夹中写入文件,文件的数据量存在最大值,达到最大值后切换文件进行写入。
* @param targetDir 目标文件夹(目录)
* @param maxSize 文件的数据最大值
* @param charset 文件采用的字符编码
*/
public ShiftWriter(File targetDir, long maxSize, String charset) throws FileNotFoundException, UnsupportedEncodingException {
if(maxSize<=0)throw new IllegalArgumentException("maxSize is wrong.");
if(targetDir==null
------解决方案--------------------
!targetDir.exists()
------解决方案--------------------
!targetDir.isDirectory()){
throw new IllegalArgumentException("target dir isn't exists.");
}
this.maxSize = maxSize;
this.charset = charset;
this.targetDir = targetDir;
File targets[] = targetDir.listFiles();
Pattern pattern = Pattern.compile("(\\d+)\\.(txt)");
for(File file : targets){
Matcher matcher = pattern.matcher(file.getName());
if(matcher.matches()){
int index = Integer.parseInt(matcher.group(1));
fileIndex = index>fileIndex?index:fileIndex;
}
}
shiftTarget();
}
private File targetFile;
private BufferedOutputStream buffer;
private CounterOutputStream counter;
private OutputStreamWriter writer;
private void shiftTarget() throws FileNotFoundException, UnsupportedEncodingException {
do{
targetFile = new File(targetDir,fileIndex+".txt");
if(targetFile.exists() && targetFile.isFile()){
if(targetFile.length()>=maxSize){
fileIndex++;
continue;
}
}
break;
}while(true);
OutputStream output = new FileOutputStream(targetFile,true);
buffer = new BufferedOutputStream(output);
counter = new CounterOutputStream(buffer);
writer = new OutputStreamWriter(counter, charset);
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
writer.write(cbuf, off, len);
writer.flush();
if(counter.count>=maxSize){
writer.close();
buffer.flush();
buffer.close();
fileIndex++;
shiftTarget();
}
}
@Override
public void flush() throws IOException {
writer.flush();
buffer.flush();
}
@Override
public void close() throws IOException {
writer.close();
buffer.close();
}
/** 写入文件。将当前文件的数据写入到Writer中。 */
private static void writeFile(File src, Writer writer) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(src), "GBK"));
int ch = -1;
try{
while((ch=br.read())!=-1){
writer.write(ch);
}
}finally{