package day0609;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class Difined {
public static void testCopy(String src,String dst)throws IOException{
File srcFile=new File(src);
if(!srcFile.exists()){
throw new CopyException("源文件不存在");
}
if(srcFile.isDirectory()){
throw new CopyException("源文件不能是文件夹");
}
File dstFile=new File(dst);
if(dstFile.exists()&&dstFile.isDirectory()){
dstFile=new File(dstFile,srcFile.getName());
}
FileInputStream in=null;
FileOutputStream out=null;
try{
in=new FileInputStream(srcFile);
out=new FileOutputStream(dstFile);
int b;
byte[] buf=new byte[8*1024];
while((b=in.read(buf))!=-1){
out.write(buf,0,b);
}
}catch(IOException e){
e.printStackTrace();
throw new CopyException("复制失败",e);//////////////
}finally{
in.close();
out.close();
}
}
@Test
public void testCP1(){
/**
* /dfg.png不能被写
*/
String src="/home/baohan/桌面/111.png";
String dst="/home/baohan/桌面/dfg.png";////////////////////////////
try{
testCopy(src,dst);
}catch(IOException e){
e.printStackTrace();
System.out.println(e.getCause());
}
}
}
class CopyException extends IOException{
public CopyException(String str){
super(str);
}
public CopyException(String str, Throwable cause){
super(str,cause);
}
}
大家看看注释的那段,我的dfg.png是能读不能写的,所以会抛出异常,下面我又重抛出我的异常,加上原因,但是运行后没有打印出原因!
java.io.FileNotFoundException: /home/baohan/桌面/dfg.png (权限不够)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at day0609.Difined.testCopy(Difined.java:37)
at day0609.Difined.testCP1(Difined.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
------解决方案--------------------
你没有调用自己的异常啊?你该这样写:
public void testCP1() throws CopyException{
String src="/home/baohan/桌面/111.png";
String dst="/home/baohan/桌面/dfg.png";
testCopy(src,dst);
}