Java合并文本文件

按字符流和字节流方式分别有两个构造函数进行追加式打开:


FileWriter(String fileName, boolean append)
FileOutputStream(String name, boolean append)

其实就是在我文章的方法4或方法9进行改造;

https://blog.csdn.net/nianbingsihan/article/details/80269475

字符流式追加合并:

/**
     * 使用缓冲字符流合并文本文件
     * @param srcFilePath 源文件列表
     * @param destFilePath 新文件
     */
    public static void mergeTextFileByCharacter(String[] srcFilePath,String destFilePath){
    	File destFile = new File(destFilePath);
    	if(destFile.exists()){
    		destFile.delete();
    	}
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        for(int i=0; i<srcFilePath.length; i++){
        	try{
                fileReader = new FileReader(srcFilePath[i]);
                if(i==0){
                	fileWriter = new FileWriter(destFilePath);
                }
                else{
                	fileWriter = new FileWriter(destFilePath,true);
                }
                bufferedReader = new BufferedReader(fileReader);
                bufferedWriter = new BufferedWriter(fileWriter);
                char[] charArray = new char[1024];
                int lengthRead;
                while ((lengthRead = bufferedReader.read(charArray)) != -1){//读取一个字符,-1代表读到流末尾
                    bufferedWriter.write(charArray,0,lengthRead);//写入读到的字符
                }
                bufferedWriter.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(null != fileReader){
                    try{
                        fileReader.close();
                    }catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null != bufferedReader){
                    try{
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null != fileWriter){
                    try{
                        fileWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null != bufferedWriter){
                    try{
                        bufferedWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

字节流式追加合并:

/**
     * 使用缓冲字节流合并二进制文件
     * @param srcFilePath 源文件列表
     * @param destFilePath 新文件
     */
    public static void mergeBinaryFileByByte(String[] srcFilePath,String destFilePath){
    	File destFile = new File(destFilePath);
    	if(destFile.exists()){
    		destFile.delete();
    	}
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        for(int i=0; i<srcFilePath.length; i++){
        	try{
                fileInputStream = new FileInputStream(srcFilePath[i]);
                if(i==0){
                	fileOutputStream = new FileOutputStream(destFilePath);
                }
                else{
                	fileOutputStream = new FileOutputStream(destFilePath,true);
                }
                bufferedInputStream = new BufferedInputStream(fileInputStream);
                bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
     
                byte[] byteArray = new byte[1024];
                int readLength;//每次读取的长度
                //
                /**
                 * 每次读取byteArray.length个字符存入byteArray,-1代表已经到流的末尾
                 * readLength为读取的长度,一般为byteArray.length,最后一次循环时一般不足byteArray.length,为实际读取长度
                 */
                while((readLength = bufferedInputStream.read(byteArray)) != -1){
                    bufferedOutputStream.write(byteArray,0,readLength);//写入读取的字节数组.
                }
                bufferedOutputStream.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(null != fileInputStream){
                    try{
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null != fileOutputStream){
                    try{
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null != bufferedInputStream){
                    try{
                        bufferedInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(null != bufferedOutputStream){
                    try{
                        bufferedOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值