标签:
package Download;
import java.io.File;
package Download;
import java.io.*;
public class hello {
public static void main(String args[]) throws FileNotFoundException{
System.out.println("hello");
new download(1,0,"aaa").start();
new download(2,3,"bbb").start();
new download(3,6,"ccc").start();
}
}
class download extends Thread{
protected int ID;
protected int start;
protected String str;
public download(int ID,int start ,String string) {
this.ID=ID;
this.start=start;
this.str=string;
}
public void run(){
System.out.println(str);
try {
Thread.sleep(ID*2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
//只new一个文件---------------------------------------------------------
File fileA =new File("aaa.txt");
//new一个文件,并写入数据------------------------------------------------
File fileB =new File("bbb.txt");
if(fileB.exists()){
FileInputStream fis=new FileInputStream(fileB);
byte b[]=new byte[1024];
int len=0;
while((len=fis.read(b))!=-1){
String temp=new String(b);
System.out.println(temp);
}
fis.close();
}
FileOutputStream fos=new FileOutputStream(fileB);
fos.write(str.getBytes(),0,str.length());
fos.close();
//new一个文件和一个RandomAccessFile,用RandomAccessFile写入数据---------
File fileC =new File("ccc.txt");
RandomAccessFile raf=new RandomAccessFile(fileC,"rwd");
raf.seek(start);
raf.write(str.getBytes(),0,str.length());
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果:
创建了两个文件
bbb.txt 内容:ccc
ccc.txt 内容:aaabbbccc
运行结果
hello
aaa
bbb
ccc
aaa
bbb
标签:
原文地址:http://www.cnblogs.com/324sige/p/5726967.html