java作业急!!!!!求大神解答。
java作业急!!!!!求大神解答。①自定义线程类②一个线程利用FileInputStream和FileOutputStream完成文件的复制③利用FileReader和...
java作业急!!!!!求大神解答。①自定义线程类
②一个线程利用FileInputStream和 FileOutputStream完成文件的复制
③利用FileReader和FileWriter类完成文件的复制功能
④两个线程分别完成一个文件复制的功能 展开
②一个线程利用FileInputStream和 FileOutputStream完成文件的复制
③利用FileReader和FileWriter类完成文件的复制功能
④两个线程分别完成一个文件复制的功能 展开
1个回答
展开全部
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();
}
追问
谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询