IO 文件合并
来源:互联网 发布:linux vlan 编辑:程序博客网 时间:2024/04/29 15:44
package com.zyf.day22;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.Vector;//合并文件public class demo1 { public static void main(String[] args) throws IOException { merge3();} public static void merge1() throws IOException{ //找到目标文件 File inFile1 = new File("c:\\a.txt"); File inFile2 = new File("c:\\b.txt"); File outFile = new File("c:\\c.txt"); //建立数据的输入输出通道 FileInputStream fileInputStream1 = new FileInputStream(inFile1); FileInputStream fileInputStream2 = new FileInputStream(inFile2); FileOutputStream fileOutputStream = new FileOutputStream(outFile); ArrayList<FileInputStream> list = new ArrayList<FileInputStream>(); list.add(fileInputStream1); list.add(fileInputStream2); byte[] buf = new byte[1024]; int length = 0; for(int i = 0;i < list.size();i++){ FileInputStream fileInputStream = list.get(i); while((length = fileInputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); } //关闭资源 fileInputStream.close(); } fileOutputStream.close(); } public static void merge2() throws IOException{ //找到目标文件 File inFile1 = new File("c:\\a.txt"); File inFile2 = new File("c:\\b.txt"); File outFile = new File("c:\\c.txt"); //建立数据的输入输出通道 FileInputStream fileInputStream1 = new FileInputStream(inFile1); FileInputStream fileInputStream2 = new FileInputStream(inFile2); FileOutputStream fileOutputStream = new FileOutputStream(outFile); //建立序列流对象 SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2); byte[] buf = new byte[1024]; int length = 0; while((length = inputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); } //关闭资源 inputStream.close(); fileOutputStream.close(); } public static void merge3() throws IOException{ //找到目标文件 File inFile1 = new File("c:\\a.txt"); File inFile2 = new File("c:\\b.txt"); File inFile3 = new File("c:\\d.txt"); File outFile = new File("c:\\c.txt"); //建立数据的输入输出通道 FileInputStream fileInputStream1 = new FileInputStream(inFile1); FileInputStream fileInputStream2 = new FileInputStream(inFile2); FileInputStream fileInputStream3 = new FileInputStream(inFile3); FileOutputStream fileOutputStream = new FileOutputStream(outFile); //创建序列流对象 Vector<FileInputStream> vector = new Vector<FileInputStream>(); vector.add(fileInputStream1); vector.add(fileInputStream2); vector.add(fileInputStream3); Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream sequenceInputStream = new SequenceInputStream(e); //读取文件数据 byte[] buf = new byte[1024]; int length = 0; while((length = sequenceInputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); } //关闭资源 sequenceInputStream.close(); fileOutputStream.close(); }}
0 0
- IO 文件合并
- Java io 文件读取 合并
- 52-IO流(文件切割) 53-IO流(文件合并)
- 黑马程序员-java IO的文件合并
- IO流--切割与合并文件
- java io流 切割合并文件
- Java IO流文件切割 与合并
- 【java基础:IO】文件合并(mergefiles)Demostration
- 24-IO流-53-IO流(文件合并)
- 24-IO流-54-IO流(文件切割合并+配置文件)
- 使用 IO 流,将多个文件合并成一个文件
- 使用 IO 流,将多个文件合并成一个文件
- IO流 应用: 文件的 切割与合并
- java IO操作 按照规律合并两个txt文件
- 【java】IO流之切割与合并文件
- 用IO流去切割与合并文件
- IO流--SequenceInputStream序列化流的应用--文件合并
- 黑马程序员 IO流 文件的分割与合并
- android移动支付——PayPal支付
- php之文字水印-图片水印-图片裁剪
- 安装ADT的时候,提示“Cannot complete the install because one or more required items could not be
- 蓝牙解析(part6):BLE地址类型
- GZIP压缩解压类
- IO 文件合并
- win7+eclipse+mapreduce环境搭建
- 数组求和leetcode
- Java基本语法
- 448. Find All Numbers Disappeared in an Array
- 最小二乘法求色彩还原矩阵
- jquery.from.js 上传excel用法小结
- 服务端进程突然奔溃客会发生什么?
- 楼层效果