java作业急!!!!!求大神解答。

java作业急!!!!!求大神解答。①自定义线程类②一个线程利用FileInputStream和FileOutputStream完成文件的复制③利用FileReader和... java作业急!!!!!求大神解答。①自定义线程类
②一个线程利用FileInputStream和 FileOutputStream完成文件的复制
③利用FileReader和FileWriter类完成文件的复制功能
④两个线程分别完成一个文件复制的功能
展开
 我来答
sunnylxyhy2012
2016-12-13 · 超过10用户采纳过TA的回答
知道答主
回答量:29
采纳率:0%
帮助的人:14.2万
展开全部
public static void main(String[] args) {

String path = "被复制的文件路径";
String path01 = "";
String path02 = "";
final File file = new File(path);
final File file01 = new File(path01);
final File file02 = new File(path02);
new Thread(new Runnable() {

public void run() {

FileInputStream fis = null;
FileOutputStream fos = null;

try {
fis = new FileInputStream(file);
fos = new FileOutputStream(file01);

byte[] temp = new byte[1024];
int length = 0;
while ((length = fis.read(temp)) != -1) {
fos.write(temp, 0, length);
}
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {

try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {

try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}
}).start();


new Thread(new Runnable() {

public void run() {

FileReader fr = null;
FileWriter fw = null;

try {
fr = new FileReader(file);
fw = new FileWriter(file02, true);
char[] temp = new char[1024];
int length = 0;
if((length=fr.read(temp)) != -1){
fw.write(temp,0,length);
}
fw.flush();
}  catch (IOException e) {
e.printStackTrace();
}finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}).start();

}
追问
谢谢
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式