package com.zyf.day22; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; //把一首mp3切成n份 public class demo2 { public static void main(String[] args) throws IOException { mergeFile(); } //合并 public static void mergeFile() throws IOException{ //找到目标文件 File dir = new File("c:\\mp3"); Vector<FileInputStream> vector = new Vector<FileInputStream>(); File[] files = dir.listFiles(); for(File file: files){ if(file.getName().endsWith(".mp3")){ vector.add(new FileInputStream(file)); } } //通过Vector获取迭代器 Enumeration<FileInputStream> e = vector.elements(); //创建序列流 SequenceInputStream inputStream = new SequenceInputStream(e); FileOutputStream fileOutputStream = new FileOutputStream("c:\\mp3\\合并.mp3"); //建立缓冲数组读取文件 byte[] buf = new byte[1024]; int length = 0; while((length = inputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); } //关闭资源 fileOutputStream.close(); inputStream.close(); } //切割mp3 public static void cutFile() throws IOException{ File file = new File("c:\\mp3\\Adele - Rolling In The Deep.mp3"); File dir = new File("c:\\mp3"); FileInputStream fileInputStream = new FileInputStream(file); byte[] buf = new byte[1024 * 1024]; int length = 0; for(int i=0;(length = fileInputStream.read(buf))!= -1;i++){ FileOutputStream fileOutputStream = new FileOutputStream(new File(dir,"part" + i + ".mp3")); fileOutputStream.write(buf,0,length); fileOutputStream.close(); } //关闭资源 fileInputStream.close(); } }
IO 文件拆分
最新推荐文章于 2024-05-21 23:46:46 发布