Sunteți pe pagina 1din 2

Data Save

============================================

import javax.swing.*;
import java.io.*;
import java.util.*;

class Save extends Thread{

public Save(JTextArea P){


ta=P;
}
public void run(){
while(flag){
int count=Token();
autoSave(count);
try{
Thread.sleep(1000);
}catch(Exception e){e.printStackTrace();}
}
}

public int Token(){


int count=0;
String s1="",s2="",s3="";
String data=ta.getText();
StringTokenizer st=new StringTokenizer(data," ");
while(st.hasMoreTokens()){
String key=st.nextToken();
if(key.equals("osama"))
s1="osama ";
else if(key.equals("bin"))
s2="bin ";
else if(key.equals("ladin"))
s3="ladin";
String s=s1+s2+s3;
if(s.equals("osama bin ladin")){
count++;
s=s1=s2=s3="";
}
}
return count;
}

public void autoSave(int count){


try{
String data=ta.getText();
file=new FileOutputStream("new.txt");
if(data.endsWith("end"))
flag=false;
String str=""+count;
file.write(str.getBytes());
file.close();
}catch(Exception e){e.printStackTrace();}
}
private FileOutputStream file;
private JTextArea ta=new JTextArea();
private volatile boolean flag=true;
}

class GUI{
public GUI(){
f=new JFrame();
f.setSize(400,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ta=new JTextArea();
f.add(ta);
f.show(true);
}
JTextArea ta;
private JFrame f;
}

class Main{
public static void main(String a[]){
GUI g=new GUI();
Save s=new Save(g.ta);
s.start();
}
}

http://www.ravianeducation.blogspot.com
FARHAN: 03008855006

S-ar putea să vă placă și