/*
需求:将一个Mp3文件分割成4个,然后在合并,并试着播放该合并后的文件;
思路:将该文件作为源读取,然后输出到目的文件中去,这是整体的思路
整个操作的都是字节流信息;类似于复制文件的操作
源:InputStream,Reader
是否为纯文本?否,InputStream,FileInputStream
设备:硬盘文件,是否要提高效率?是
最终选:BufferedInputStream
目的:OutputStream,Writer
是否为纯文本?否 OutputStream,是否要提高效率?是
最终选择BufferedOutputStream
步骤:
*/
package splitmp3;
import java.io.*;
import java.util.*;
class SplitMp3
{
public static void main(String []args)
{
combineMp3();
}
public static void combineMp3()
{
Vector<FileInputStream> vec=new Vector<FileInputStream>();
try
{
for(int i=1;i!=5;i++)
vec.add(new FileInputStream(""+i+".part"));
}
catch(IOException ioe )
{
throw new RuntimeException("文件输入流打开失败");
}
Enumeration<FileInputStream> em=vec.elements();
SequenceInputStream sis=null;
File file=new File("范玮琪 - 一个像夏天一个像秋天.mp3");
try
{
if(!file.exists())
file.createNewFile();
}
catch(IOException ioe)
{
throw new RuntimeException("mp3文件创建失败");
}
BufferedOutputStream bos=null;
try
{
sis=new SequenceInputStream(em);
bos=new BufferedOutputStream(new FileOutputStream(file));
byte[]buf=new byte[1024*1024];
int len=0;
while((len=sis.read(buf))!=-1)
{
bos.write(buf,0,len);
}
}
catch(IOException ioe)
{
throw new RuntimeException("读文件流链打开失败");
}
finally
{
try
{
if(bos!=null)
bos.close();
}
catch(IOException ioe)
{
throw new RuntimeException("写文件输出流关闭失败");
}
try
{
if(sis!=null)
sis.close();
}
catch(IOException ioe)
{
throw new RuntimeException("读文件输入流链关闭失败");
}
}
}
public static void splitMp3()
{
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try
{
bis=new BufferedInputStream(new FileInputStream("E:\\Kugou\\KuGou\\范玮琪 - 一个像夏天一个像秋天.mp3"));
int len=0;
byte[]buf=new byte[1024*1024];
int count=1;
while((len=bis.read(buf))!=-1)
{
bos=new BufferedOutputStream(new FileOutputStream(""+(count++)+".part"));
bos.write(buf,0,len);
}
}
catch(IOException ioe)
{
throw new RuntimeException("读写文件失败");
}
finally
{
try
{
if(bis!=null)
bis.close();
}
catch(IOException ioe)
{
throw new RuntimeException("读文件关闭异常。");
}
try
{
if(bis!=null)
bis.close();
}
catch(IOException ioe)
{
throw new RuntimeException("写文件关闭异常。");
}
}
}
}
如此多的RuntimeException,该怎么优化,有什么解决方案么?代码运行没问题的。。
需求:将一个Mp3文件分割成4个,然后在合并,并试着播放该合并后的文件;
思路:将该文件作为源读取,然后输出到目的文件中去,这是整体的思路
整个操作的都是字节流信息;类似于复制文件的操作
源:InputStream,Reader
是否为纯文本?否,InputStream,FileInputStream
设备:硬盘文件,是否要提高效率?是
最终选:BufferedInputStream
目的:OutputStream,Writer
是否为纯文本?否 OutputStream,是否要提高效率?是
最终选择BufferedOutputStream
步骤:
*/
package splitmp3;
import java.io.*;
import java.util.*;
class SplitMp3
{
public static void main(String []args)
{
combineMp3();
}
public static void combineMp3()
{
Vector<FileInputStream> vec=new Vector<FileInputStream>();
try
{
for(int i=1;i!=5;i++)
vec.add(new FileInputStream(""+i+".part"));
}
catch(IOException ioe )
{
throw new RuntimeException("文件输入流打开失败");
}
Enumeration<FileInputStream> em=vec.elements();
SequenceInputStream sis=null;
File file=new File("范玮琪 - 一个像夏天一个像秋天.mp3");
try
{
if(!file.exists())
file.createNewFile();
}
catch(IOException ioe)
{
throw new RuntimeException("mp3文件创建失败");
}
BufferedOutputStream bos=null;
try
{
sis=new SequenceInputStream(em);
bos=new BufferedOutputStream(new FileOutputStream(file));
byte[]buf=new byte[1024*1024];
int len=0;
while((len=sis.read(buf))!=-1)
{
bos.write(buf,0,len);
}
}
catch(IOException ioe)
{
throw new RuntimeException("读文件流链打开失败");
}
finally
{
try
{
if(bos!=null)
bos.close();
}
catch(IOException ioe)
{
throw new RuntimeException("写文件输出流关闭失败");
}
try
{
if(sis!=null)
sis.close();
}
catch(IOException ioe)
{
throw new RuntimeException("读文件输入流链关闭失败");
}
}
}
public static void splitMp3()
{
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try
{
bis=new BufferedInputStream(new FileInputStream("E:\\Kugou\\KuGou\\范玮琪 - 一个像夏天一个像秋天.mp3"));
int len=0;
byte[]buf=new byte[1024*1024];
int count=1;
while((len=bis.read(buf))!=-1)
{
bos=new BufferedOutputStream(new FileOutputStream(""+(count++)+".part"));
bos.write(buf,0,len);
}
}
catch(IOException ioe)
{
throw new RuntimeException("读写文件失败");
}
finally
{
try
{
if(bis!=null)
bis.close();
}
catch(IOException ioe)
{
throw new RuntimeException("读文件关闭异常。");
}
try
{
if(bis!=null)
bis.close();
}
catch(IOException ioe)
{
throw new RuntimeException("写文件关闭异常。");
}
}
}
}
如此多的RuntimeException,该怎么优化,有什么解决方案么?代码运行没问题的。。
推荐优化的也就关闭资源的两个try{}catch而已,但是基本算是没优化。
try
{
if(bis!=null){
bis.close();
}if(bis!=null){
bis.close();
}
}
catch(IOException ioe)
{
throw new RuntimeException("关闭资源失败。");
}
把上面的代码放在你的finally里面就行了。为什么优化这里呢?因为一般情况下只要资源不是空,那么关闭它就不会出错,反正我没遇到过出错的情况。
当然,如果你不想要错误提示那么精确你也可以按照我这个方法自己整合一下。
就是把所有的可能出错的地方用一个
try{
//.......你的所有需要try的代码
} catch(Exception e){//用这个的话所有的异常都会捕获,当然你也可以换成IOException,不过那样别的异常你就得另写try了
}
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
你定义的一个是bis一个是bos啊...
我直接复制你的代码修改的,导致我也出错了。你原来的代码也有错哦,自己改一下。关闭原则是先打开的也就是先使用的最后关闭
try {
if(bis!=null){
bis.close();
}
if(bos!=null){
bos.close();
}
} catch(IOException ioe) {
throw new RuntimeException("关闭资源失败。");
}
大部分异常不需要重复包装,直接从函数中声明往外throw出去就行了,因为你并没有解决该异常,把异常重新包装为Runtime异常,并没有对程序或者调用方带来任何好处。public static void combineMp3() throws IOException
这样的catch才是真正有效的是么?
大部分异常不需要重复包装?是什么意思 啊??
菜鸟桑不起啊!
不需要重复包装的意思就是,没必要把原有的异常又换个方式抛出去,这隐藏了有价值的信息。
本来异常时IOException,一看名字都知道,是IO操作问题,你换成RuntimeException,看了名字也完全不知道是怎么回事。