使用IO实现文件的合并

    xiaoxiao2025-05-29  10

    如果要进⾏⽂件的操作,可以使⽤FileOutputStream类来处理,它的构造方法有两种: (1)接收File类(覆盖):public FileOutputStream(File file) throws FileNotFoundException 这个方法会将原来的覆盖掉,只保留现在的。 (2)接收File类(追加):public FileOutputStream(File file, boolean append) 表示在原来的 基础上追加 ,原来的保留。 除了⽂件之外,IO的操作也可以发⽣在内存之中,这 种流称之为内存操作流。⽂件流的操作⾥⾯⼀定会产⽣⼀个⽂件数据(不管最后这个⽂件数据是否被保留)。 内存流分为两类:

    字节内存流:ByteArrayInputStream、ByteArrayOutputStream字符内存流:CharArrayReader、CharArrayWriter ByteArrayInputStream和ByteArrayOutputStream的构造⽅法: public ByteArrayInputStream(byte buff[]) public ByteArrayOutputStream()

    现在有两个⽂件:data-a.txt、data-b.txt。现在要求将这两个⽂件的内容做⼀个合并处理: 方法一: 文件追加(即使用append) 即将public FileOutputStream(File file, boolean append)中的boolean append 设为true 代码实现

    package com.zh.io; import java.io.*; public class TestMemory2 { public static void join(String[] file1,String FIle2Path ){ if(file1==null||FIle2Path==null){ throw new IllegalArgumentException("不存在"); } File file2=new File(FIle2Path); //将路径转换成文件 File parent=file2.getParentFile(); if(!parent.exists()){ parent.mkdirs(); } for(int i=0;i<file1.length;i++){ if(file1[i]==null){ throw new IllegalArgumentException(); } File files1=new File(file1[i]); try(FileInputStream in=new FileInputStream(files1); FileOutputStream out=new FileOutputStream(file2,true)) { byte[] buff=new byte[1024]; int len=-1; while((len=in.read(buff))!=-1){ out.write(buff,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws FileNotFoundException { String[] file1= {"D:"+ File.separator+"test5.25"+File.separator+"A"+File.separator+"data_a.txt","D:"+ File.separator+"test5.25"+File.separator+"A"+File.separator+"data_b.txt"}; String file2= "D:"+ File.separator+"test5.25"+File.separator+"A"+File.separator+"data.txt"; join(file1,file2); } }

    方法二: 内存流data-a.txt->ByteArrayOutputStream data-b.txt-ByteArrayOutputStream ByteArrayOutputStream byte[]->FileOutputStream 内存操作流最为核⼼的部分就是:将所有OutputStream输出的程序保存在了程序⾥⾯,所以可以通过这⼀特征实现处理 ByteArrayOutputStream memorystream=new ByteArrayOutputStream() … memorystream.write(buff,0,len); 上面的语句将数据保存在了memorystream中 但有一个缺陷:不知道文件有多大,可能造成空间资源的浪费 代码实现:

    public static void main(String[] args) { try(FileInputStream fa=new FileInputStream("D:"+ File.separator+"test5.25"+File.separator+"A"+File.separator+"data_a.txt"); FileInputStream fb=new FileInputStream("D:"+ File.separator+"test5.25"+File.separator+"A"+File.separator+"data_b.txt"); FileOutputStream fout=new FileOutputStream("D:"+ File.separator+"test5.25"+File.separator+"A"+File.separator+"data.txt"); ByteArrayOutputStream memorystream=new ByteArrayOutputStream()) { byte[] buff=new byte[1024]; int len=-1; while((len=fa.read(buff))!=-1){ memorystream.write(buff,0,len); } while (((len=fb.read(buff))!=-1)){ memorystream.write(buff,0,len); } byte[] newdata =memorystream .toByteArray(); fout.write(newdata); fout.flush(); } catch (IOException e1) { e1.printStackTrace(); } } }
    最新回复(0)