日期:2014-05-20  浏览次数:20665 次

建一个文件,再建一个txt文件,打印十行字用线程实现.我写的程序太难看了.在线等,高手帮忙
import   java.io.*;
class     FilePath{
public   void   filePath(String   filePath){
File   f=new   File(filePath);
if(!f.exists())
{
f.mkdir();
}
}
public   void   newFile(String   filePath,String   content){
try{
File   f=new   File(filePath);
if(!f.exists());{
f.createNewFile();
FileWriter   newfile=new   FileWriter(filePath,true);
  PrintWriter   myFile=new   PrintWriter(newfile,true);
myFile.println(content);
myFile.close();
}
}catch(Exception   e){
e.printStackTrace();
}
}
}
class   TestThread   implements   Runnable{
private   int   time=1;
public   void   run(){
while(time <=10){
write();

}
}
            public   synchronized   void   write()
{

if(time <11){
try{
Thread.sleep(100);
}
catch(Exception   e){
e.printStackTrace();
}


FilePath   f=new   FilePath();
f.filePath( "d://try ");
f.newFile( "d://try//try.txt ", "这是第 "+time+++ "个线程 ");
}

}

}


public   class   TestTxt{

public   static   void   main(String   []args){
TestThread   t=new   TestThread();
new   Thread().start();
new   Thread().start();
new   Thread().start();
        new   Thread().start();
new   Thread(t).start();
new   Thread(t).start();
new   Thread(t).start();
new   Thread(t).start();
new   Thread(t).start();
if(Thread.activeCount()==10){
System.out.println( "输入结束 ");
}
}
}





------解决方案--------------------
new一个线程,循环启动十次就行了
------解决方案--------------------
循环不晓得用??
在循环里new线程