java文件切割

package com.xbl.test;
002 
003import java.io.File;
004import java.io.RandomAccessFile;
005 
006import org.junit.Test;
007/**
008* <p>[SplitImageUtil]描述:文件分割工具类</p>
009* @作者 xbl
010* @日期 2011年2月25日 11:31:54
011*/
012public class SplitImageUtil {
013 
014 
015    /**
016     * <p>拆分文件</p>
017     * @param file 源文件
018     * @param count 拆分的文件个数
019     * @throws Exception
020     */
021    public static void split(String file ,int count) throws Exception
022    {      
023       
024        RandomAccessFile raf = new RandomAccessFile(new File(file),"r");
025        long length = raf.length();
026       
027        long theadMaxSize =  length / count; //每份的大小 1024 * 1000L;
028        raf.close();
029 
030        long offset = 0L;
031        for(int i=0;i< count-1;i++) //这里不去处理最后一份
032        {
033            long fbegin = offset;
034            long fend = (i+1) * theadMaxSize;
035            offset =write(file,i,fbegin,fend);
036        }
037 
038        if(length- offset>0) //将剩余的都写入最后一份
039            write(file,count-1,offset,length);
040    }
041    /**
042     * <p>指定每份文件的范围写入不同文件</p>
043     * @param file 源文件
044     * @param index 文件顺序标识
045     * @param begin 开始指针位置
046     * @param end 结束指针位置
047     * @return
048     * @throws Exception
049     */
050    private static long write(String file,int index,long begin,long end) throws Exception
051    {
052        RandomAccessFile in = new RandomAccessFile(new File(file),"r");
053        RandomAccessFile out = new RandomAccessFile(new File(file+"_"+index+".tmp"),"rw");
054        byte[] b = new byte[1024];
055        int n=0;
056        in.seek(begin);//从指定位置读取
057 
058        while(in.getFilePointer() <= end && (n= in.read(b))!=-1)
059        {
060            out.write(b, 0, n);
061        }
062        long endPointer =in.getFilePointer();
063        in.close();
064        out.close();
065        return endPointer;
066    }
067    /**
068     * <p>合并文件</p>
069     * @param file 指定合并后的文件
070     * @param tempFiles 分割前的文件名
071     * @param tempCount 文件个数
072     * @throws Exception
073     */
074    public static void merge(String file,String tempFiles,int tempCount) throws Exception
075    {
076        RandomAccessFile ok = new RandomAccessFile(new File(file),"rw");
077 
078        for(int i=0;i<tempCount;i++)
079        {
080            RandomAccessFile read = new RandomAccessFile(new File(tempFiles+"_"+i+".tmp"),"r");
081            byte[] b = new byte[1024];
082            int n=0;
083            while((n=read.read(b))!= -1)
084            {
085                ok.write(b, 0, n);
086            }
087            read.close();
088        }
089        ok.close();
090    }
091    @Test
092    public void testSplit()throws Exception
093    {
094        SplitImageUtil.split("c:\\http_imgload.jpg", 5);
095    }
096    @Test
097    public void testMerge() throws Exception
098    {
099        SplitImageUtil.merge("c:\\3dd.jpg", "c:\\http_imgload.jpg", 5);
100    }
101 
102}
Java中,可以使用Java Sound API来切割音频文件。下面是一个简单的示例代码,演示如何使用Java Sound API来切割音频文件: ```java import javax.sound.sampled.*; import java.io.File; import java.io.IOException; public class AudioCutter { public static void main(String[] args) { String inputFile = "input.wav"; // 输入音频文件路径 String outputFile = "output.wav"; // 输出音频文件路径 int startSecond = 10; // 起始时间(秒) int endSecond = 20; // 结束时间(秒) try { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(inputFile)); AudioFormat format = audioInputStream.getFormat(); long frames = audioInputStream.getFrameLength(); double durationInSeconds = (frames + 0.0) / format.getFrameRate(); // 计算起始和结束帧数 int startFrame = (int) (startSecond * format.getFrameRate()); int endFrame = (int) (endSecond * format.getFrameRate()); // 限制起始和结束帧数在有效范围内 startFrame = Math.max(0, startFrame); endFrame = Math.min((int) frames, endFrame); // 计算切割后的帧数 int cutLength = endFrame - startFrame; // 创建切割后的音频流 AudioInputStream cutAudioInputStream = new AudioInputStream(audioInputStream, format, cutLength); AudioSystem.write(cutAudioInputStream, AudioFileFormat.Type.WAVE, new File(outputFile)); System.out.println("音频切割成功!"); } catch (UnsupportedAudioFileException | IOException e) { e.printStackTrace(); } } } ``` 上述代码中,我们首先使用`AudioSystem.getAudioInputStream()`方法获取音频文件的输入流。然后,我们获取音频的格式和帧数,并根据起始和结束时间计算出对应的起始和结束帧数。接下来,我们使用`AudioInputStream`的构造函数创建一个切割后的音频流,并使用`AudioSystem.write()`方法将切割后的音频流写入到输出文件中。 请注意,上述代码仅适用于切割WAV格式的音频文件。如果要切割其他格式的音频文件,需要相应地调整代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值