1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
import java.io.*;
import java.lang.Runtime;
import java.applet.*;
public class InitFunction extends Applet {
private static final long serialVersionUID = 1L;
private String DirCmd;
private String DirFile, WriteLineString;
boolean EraseOld;
private static Object threadLockExec = new Object();
private static Object threadLockWrite = new Object();
public void init (){
( new ExecuteCmdThread() ).start();
( new WriteFileThread() ).start();
//ExecuteCmd("c:\\windows\\system32\\charmap.exe");
//WriteFile("c:\\tt.txt", "blabla", true);
}
class ExecuteCmdThread extends Thread {
public void run() {
synchronized(threadLockExec)
{
while(true)
{
try
{
threadLockExec.wait();
Runtime.getRuntime().exec(DirCmd);
}
catch( Exception ex )
{
System.out.println( "Exception in ExecuteCmd, " + ex.getMessage() );
}
}
}
}
}
class WriteFileThread extends Thread {
public void run() {
synchronized(threadLockWrite)
{
while(true)
{
try
{
threadLockWrite.wait();
PrintWriter OutputFile = new PrintWriter(new FileWriter(DirFile, !EraseOld));
OutputFile.println(WriteLineString);
OutputFile.close();
}
catch( Exception ex )
{
System.out.println( "Exception in WriteFile, " + ex.getMessage() );
}
}
}
}
}
public int WriteFile(String DirFile, String WriteLineString, boolean EraseOld) {
try
{
synchronized( threadLockWrite )
{
this.DirFile = DirFile;
this.WriteLineString = WriteLineString;
this.EraseOld = EraseOld;
threadLockWrite.notifyAll();
}
return 0;
}
catch( Exception ex )
{
System.out.println( "Exception in Write File, " + ex.getMessage() );
return -1;
}
}
public int ExecuteCmd(String cmd) {
try
{
synchronized( threadLockExec )
{
DirCmd = cmd;
threadLockExec.notifyAll();
}
return 0;
}
catch( Exception ex )
{
System.out.println( "Exception in Execute Command, " + ex.getMessage() );
return -1;
}
}
} |
Partager