当前位置:编程学习 > JAVA >>

流与文件

编写将两个文件(文件1、文件2)中的内容合并成一个新文件(文件3)。合并方法是:从文件一读取一个字节放入文件3,在从文件2读取一个字节放入文件3,如此轮流直至某一个文件读完,再将较长文件中的剩余部分读取放入至文件3。可以使用图形界面或命令行参数输入三个文件名。
--------------------编程问答-------------------- 不一定好,楼主参考一下:

import java.io.*;
import java.util.Scanner;
public class AlterFileWrite
{
public static void main(String[] rags)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please input first filename:");
String file1=scan.next();
System.out.println("Please input second filename:");
String file2=scan.next();
System.out.println("please input output filename:");
String outFile= scan.next();
FileCopy fc=new FileCopy(file1,file2,outFile);
fc.copy();
//显示文件长度。
System.out.println("length of "+file1+" is  "+fc.getLengthOfFile1());
System.out.println("length of "+file2+" is  "+fc.getLengthOfFile2());
System.out.println("length of "+outFile+" is  "+fc.getLengthOfOutFile());
}
}
class FileCopy
{
static final int LENGTH=1024; //缓冲区大小。
private String file1,file2,outFile;
private File f1,f2,of; //输入输出文件File对象引用。
private long lengthOfFile1,lengthOfFile2; //两个输入文件的大小。
private long lengthOfOutFile=0; //输出文件大小。
private int f11024,f21024; //记录第一个和第二个文件里面用多少个LENGTH长度数据块。
private int min=0; //f11024和 f21024最小的数。


public FileCopy()
{
}
public FileCopy(String file1,String file2,String outFile)
{
this.file1=file1;
this.file2=file2;
this.outFile=outFile;
init();
}
//初始化操作
//
private void init()
{
f1=new File(file1);
f2=new File(file2);
of=new File(outFile);
if(!of.exists())
{
try
{
of.createNewFile();
}
catch(IOException e)
{
e.printStackTrace();
}
}
lengthOfFile1=f1.length();
lengthOfFile2=f2.length();

f11024=(int)(lengthOfFile1/LENGTH);
f21024=(int)(lengthOfFile2/LENGTH);
min=Math.min(f11024,f21024);
}

//获得文件的大小
//
public long getLengthOfFile1()
{
return lengthOfFile1;
}

public long getLengthOfFile2()
{
return lengthOfFile2;
}

public long getLengthOfOutFile()
{
File fileOut=new File(outFile);
long lengthOfOutFile=fileOut.length();
return lengthOfOutFile;
}
//设置文件名。
//
public void setFileName(String file1,String file2,String outFile)
{
this.file1=file1;
this.file2=file2;
this.outFile=outFile;
}
//-------------------实施数据交叉输出。
//
public void copy()
{
byte[] byteBuf1=new byte[LENGTH]; //定义读写缓冲区
byte[] byteBuf2=new byte[LENGTH];
byte[] byteOut=new byte[2*LENGTH];
try
{
FileInputStream fis1=new FileInputStream(file1); //产生输入输出流对象.
FileInputStream fis2=new FileInputStream(file2);

FileOutputStream fos = new FileOutputStream(outFile);
//整块读两个文件,每个文件每次都可读出 LENGTH 字节。
//
for(int i=0;i<min;i++)
{
int length1= fis1.read(byteBuf1);
int length2= fis2.read(byteBuf2);
for(int j=0;j<LENGTH;j++)
{
byteOut[2*j]=byteBuf1[j];
byteOut[2*j+1]=byteBuf2[j];
}
fos.write(byteOut);
}
//第一个文件没有整块数据了。
//如果第一个文件一个字节都没有了,(文件大小是LENGTH的整数倍),直接写入第二文件内容。
//否则,将第一个文件内容与第二个文件交叉放入写入数组,余下的将第二个文件内容补上。
//之后,把第二个文件的其余内容,写入到输出文件。
if(f11024<f21024)
{
int length1= fis1.read(byteBuf1);
int length2= fis2.read(byteBuf2);
//第一个文件有不足LENGTH长度数据。
//
if(length1!=-1)
{
for(int i=0;i<length1;i++) //length1 的长度,两个文件都有的部分,交叉写入。
{
byteOut[2*i]=byteBuf1[i];
byteOut[2*i+1]=byteBuf2[i];
}
for(int i=length1;i<length2;i++)
{
byteOut[2*length1+i-length1]=byteBuf2[i];
}
fos.write(byteOut,0,2*length1+(length2-length1));
}
else //第一个文件没有一个字节了。直接写入第二个文件的内容。
{
fos.write(byteBuf2,0,length2);
}
//读出并写入第二个文件的余下内容。
//
for(int i=0;i<f21024-f11024;i++)
{
while((length2=fis2.read(byteBuf2))!=-1)
{
fos.write(byteBuf2,0,length2);
}
}
}
//同上一段。
//第二个文件没有整块数据了。如果第二个文件一个字节都没有了,(文件大小是LENGTH的整数倍),直接写入第一个文件内容。
//否则,将第一个文件内容与第二个文件交叉放入写入数组,将第一个文件余下的内容补上。
//之后,把第一个文件的其余内容,写入到输出文件。
else if(f11024>f21024)
{
int length1= fis1.read(byteBuf1);
int length2= fis2.read(byteBuf2);
//第二个文件有不足LENGTH长度数据。
//
if(length2!=-1)
{
for(int i=0;i<length2;i++) //length2 的长度,两个文件都有的部分,交叉写入。
{
byteOut[2*i]=byteBuf1[i];
byteOut[2*i+1]=byteBuf2[i];
}
for(int i=length2;i<length1;i++) //注意这些数组下标。第二个文件没有了,补足第一个文件的内容。
{
byteOut[length2+i]=byteBuf1[i];//缓冲区1超出的部分.
}
fos.write(byteOut,0,2*length2+(length1-length2));
}
//第二个文件没有一个字节了。直接写入第一个文件的内容。
else
{
fos.write(byteBuf1,0,length1);
}
//读出并写入第一个文件的余下内容。
//
for(int i=0;i<f11024-f21024;i++)
{
while((length1=fis1.read(byteBuf1))!=-1)
{
fos.write(byteBuf1,0,length1);
}
}
}
//两个文件,都没有整块数据了,两个文件都是最后一块,都不足LENGTH长度。
//
else//f11024==f21024
{
int length1= fis1.read(byteBuf1);
int length2= fis2.read(byteBuf2);
//两个文件都有内容。
//
if(length1!=-1&&length2!=-1)
{
if(length1>=length2)
{
for(int i=0;i<length2;i++) //length2 的长度
{
byteOut[2*i]=byteBuf1[i];
byteOut[2*i+1]=byteBuf2[i];
}
for(int i=length2;i<length1;i++)
{
byteOut[2*length2+i-length2]=byteBuf1[i];
}
fos.write(byteOut,0,2*length2+(length1-length2));
}
else//length1<length2
{
for(int i=0;i<length1;i++) //length2 的长度
{
byteOut[2*i]=byteBuf1[i];
byteOut[2*i+1]=byteBuf2[i];
}
for(int i=length1;i<length2;i++)
{
byteOut[2*length1+i-length1]=byteBuf2[i];
}
fos.write(byteOut,0,2*length1+(length2-length1));
}
}
//文件1有内容,文件2空了。
else if(length1==-1&&length2!=-1)
{
fos.write(byteBuf2,0,length2);
}
//文件2有内容,文件1空了。
else if(length2==-1&&length1!=-1)
{
fos.write(byteBuf1,0,length1);
}
//两个文件都没内容了。不用写入了。
else
{
}
}
fis1.close();
fis2.close();
fos.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}//end copy
}//end class FileCopy.
--------------------编程问答-------------------- 这样的话如果文件稍微大点的话效率就会很低下。 --------------------编程问答-------------------- 用缓冲流会提高吗? --------------------编程问答-------------------- 除
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,