如何用Java实现两个文件的拼接

用Java语言实现两个文件的拼接与上一篇用Java实现两个文件的异或使用的方法都一样,都是FileInputStream()与FileOutputStream()方法,两个相同大小的文件a,b,把文件b拼接在文件a之后,输出的文件名为outfile具体代码详见下述内容:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class Append 
{
    static int count=0;
    static int countb=0;

    public static void main(String args[]) throws IOException
    {
         FileInputStream filea = new FileInputStream("d:\\JavaXor\\a");
         FileInputStream fileb = new FileInputStream("d:\\JavaXor\\b");
         File outfile=new File("d:\\JavaXor\\outfile");
         int filesizea=filea.available();//计算文件的大小
         int filesizeb=fileb.available();
         FileOutputStream fos=new FileOutputStream(outfile);

         int hasReada = 0;
         int hasReadb=0;

         byte[] bufa=new byte[1024];
         byte[] bufc=new byte[1024];
         byte[] buf_yua=new byte[filesizea%1024];
         byte[] buf_yub=new byte[filesizeb%1024];

         while(  (hasReada=filea.read(bufa) )>0 )
           {
            if(count<filesizea-filesizea%1024)
            {   
                for(int i=0;i<bufa.length && count<filesizea-filesizea%1024;i++)
                 {

                    bufc[i]=(byte)(bufa[i] & 0xFF);
                    count++;

                 }
                fos.write(bufc);
            }
            else if(count>=filesizea-filesizea%1024 && count<filesizea)
            {

                for(int j=0; count>=filesizea-filesizea%1024 && count<filesizea ;j++)
                {
                    buf_yua[j]=(byte)(bufa[j] & 0xFF);
                    count++;

                }
                fos.write(buf_yua);
            }

           }

         while(  (hasReadb=fileb.read(bufa) )>0 )
           {
            if(countb<filesizeb-filesizeb%1024)
            {   
                for(int i=0;i<bufa.length && countb<filesizeb-filesizeb%1024;i++)
                 {

                    bufc[i]=(byte)(bufa[i] & 0xFF);
                    countb++;

                 }
                fos.write(bufc);
            }
            else if(countb>=filesizeb-filesizeb%1024 && countb<filesizeb)
            {

                for(int j=0; countb>=filesizeb-filesizeb%1024 && countb<filesizeb ;j++)
                {
                    buf_yub[j]=(byte)(bufa[j] & 0xFF);
                    countb++;

                }
                fos.write(buf_yub);
            }



           } 

    }


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java图片拼接可以通过以下步骤实现: 1. 读取需要拼接的图片,可以使用`ImageIO.read()`方法将图片读取为`BufferedImage`对象。 2. 创建一个新的`BufferedImage`对象,用于存储拼接后的图片。需要根据拼接图片的数量和大小来设置新图片的宽度和高度。 3. 使用`Graphics2D`对象将多张图片拼接到新的`BufferedImage`对象上。可以使用`drawImage()`方法将每张图片绘制到新的`BufferedImage`对象上。 4. 保存拼接后的图片。可以使用`ImageIO.write()`方法将新的`BufferedImage`对象保存为图片文件。 以下是一个简单的示例代码: ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageJoiner { public static void main(String[] args) throws IOException { // 读取需要拼接的图片 BufferedImage img1 = ImageIO.read(new File("image1.jpg")); BufferedImage img2 = ImageIO.read(new File("image2.jpg")); // 计算拼接后图片的宽度和高度 int width = img1.getWidth() + img2.getWidth(); int height = Math.max(img1.getHeight(), img2.getHeight()); // 创建新的BufferedImage对象 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 将图片拼接到新的BufferedImage对象上 Graphics2D g2d = result.createGraphics(); g2d.drawImage(img1, 0, 0, null); g2d.drawImage(img2, img1.getWidth(), 0, null); g2d.dispose(); // 保存拼接后的图片 ImageIO.write(result, "jpg", new File("result.jpg")); } } ``` 以上代码将两张图片拼接成一张,保存为`result.jpg`文件。你可以根据自己的需求修改代码来实现更复杂的拼接操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值