IO+线程–>复制文件
public class Test {
@Test
public void test ( ) throws IOException {
turnFile ( new File ( "E:\\学习文件\\shimmer" ) , new File ( "E:\\学习文件\\SHIMMER" ) ) ;
}
public void turnFile ( File source, File target) throws IOException {
ExecutorService pool = newFixedThreadPool ( 5 ) ;
String prefix = target. getPath ( ) ;
String suffix;
if ( ! source. exists ( ) ) return ;
File[ ] files = source. listFiles ( ) ;
if ( files == null || files. length == 0 ) return ;
for ( File file : files) {
suffix = "\\" + file. getName ( ) ;
if ( file. isFile ( ) ) {
pool. execute ( new CopyFile ( file, prefix + suffix) ) ;
} else {
File targetT = new File ( ( prefix + suffix) ) ;
if ( targetT. mkdir ( ) ) {
turnFile ( file, targetT) ;
} else {
System. out. println ( "创建目标文件失败!" ) ;
}
}
}
try {
Thread. sleep ( 5000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
class CopyFile extends Thread {
File source;
File target;
public CopyFile ( File source, String target) {
if ( source. exists ( ) && target != null) {
this . source = source;
this . target = new File ( target) ;
}
}
@Override
public void run ( ) {
try {
InputStream is = new FileInputStream ( source) ;
OutputStream os = new FileOutputStream ( target, true ) ;
BufferedInputStream bis = new BufferedInputStream ( is) ;
BufferedOutputStream bos = new BufferedOutputStream ( os) ;
byte [ ] b = new byte [ 1024 ] ;
while ( bis. read ( b) != - 1 ) {
bos. write ( b) ;
bos. flush ( ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}