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("输入结束");
}
}
}
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线程