import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStreamimport java.io.IOException
import java.io.InputStream
import java.io.OutputStreampublic class CopyFiles implements Runnable {
/**
* 要复值到的地址
*/
private String toaddr
/**
* 复制源
*/
private String source
/**
*
* @param toaddr 要复值到的地址
* @param source 复制源
*/
public CopyFiles(String toaddr, String source) {
super()
this.toaddr = toaddr
this.source = source
} private void copyFile( ) throws Exception { File f1 = new File(this.toaddr)
if(!f1.exists()){
f1.mkdir()
}
File source1 = new File(this.source)
if(source1.isDirectory()){
File t = new File(f1, source1.getName())
t.mkdir()
File[] files = source1.listFiles()
if (files != null) {
this.getfiles(files, t)
}
}else{
File[] files = source1.listFiles()
if (files != null) {
this.getfiles(files, f1)
}
}
} private void getfiles(File[] fs, File f) {
for (File file : fs) {
if (file.isDirectory()) {
File[] fi = file.listFiles()
File t = new File(f, file.getName())
t.mkdir()
if (fi != null) {
getfiles(fi, t)
}
} else {
InputStream in = null
OutputStream out1 = null
System.err.println(new File(f, file.getName()).getName())
try {
in = new FileInputStream(file)
System.err.println(f.getPath())
out1 = new FileOutputStream(new File(f, file.getName()))
byte[] b = new byte[1024]
int i = 0
while ((i = in.read(b)) >0) {
out1.write(b, 0, i)
out1.flush()
}
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
out1.close()
in.close()
} catch (IOException e) {
e.printStackTrace()
}} }
}
} public void run() {
try {
copyFile()
} catch (Exception e) {
e.printStackTrace()
}
} public String getToaddr() {
return toaddr
} public void setToaddr(String toaddr) {
this.toaddr = toaddr
} public String getSource() {
return source
} public void setSource(String source) {
this.source = source
}
}
这是我写的不知道是不是你想要的下面是测试类package junit.testpublic class Test {
public static void main(String[] args) {
CopyFiles files = new CopyFiles("D:","C:\\新建文件夹")
Thread t = new Thread(files)
t.start()
}
}
package DuoXianChengimport java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.io.Reader
public class CopyFile implements Runnable {
public synchronized void run(){
try {
InputStream is=
new FileInputStream("f:"+File.separator+"file1"+File.separator+"321.txt")
Reader rd=new InputStreamReader(is,"gbk")
BufferedReader br=new BufferedReader(rd)
OutputStream os=
new FileOutputStream("f:"+File.separator+"file1"+File.separator+"1234.txt")
OutputStreamWriter osw=new OutputStreamWriter(os,"gbk")
String str=""
while((str=br.readLine())!=null){
osw.write(str)
System.out.println(str)
}
osw.close()
br.close()
os.close()
rd.close()
is.close()
System.out.println(Thread.currentThread().getName()+":复制完毕")
} catch (Exception e) {
e.printStackTrace()
}
}
}
主函数:
package DuoXianCheng
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
public class CopyFileText {
public static void main(String[] args) {
CopyFile cf=new CopyFile()
ExecutorService es=
Executors.newFixedThreadPool(3)
es.execute(cf)
es.execute(cf)
es.execute(cf)
es.shutdown()
}
}
看场景;效率的瓶颈不在代码的时候,比如用的最多的io *** 作,
下载器,下载服务器每个接口就给你500k的速度,那多线程相当于500*n,本地网络最大2m每秒,可以开3~5个线程自然快;
复制器,windows *** 作系统复制文件很慢,因为负责复制的api防止系统卡死每个线程就给你那点速度,如果用java写个多线程io流复制,速度快8倍左右;
这样的场合有个特点,速度或者说效率的关键不是java的处理能力,而是接口限制成了瓶颈;
举个反例,如果对一个集合进行遍历,打印value,使用多线程明显比单线程效率低;因为时间过多的消耗在了创建线程,销毁线程上,执行的有用代码和单线程没区别,效率不如单线程;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)