package job;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;

public class MainJob extends Thread {
    
private String path = null;

    
public MainJob(String filePath) {
        
this.path = filePath;
    }

    
public void run() {
        
while (true) {
            
try {
                write(
"Write " + (new SimpleDateFormat("yyyyMMddHHmmss")).format((new Date())));
                Thread.sleep(
3000L);
            } 
catch (InterruptedException e) {
                utils.Log.add(
this).error(e.getMessage());
            }
        }
    }

    
private void write(String msg) {
        
try {
            String path 
= String.format("%slogs\\msg.txt"this.path);
            FileWriter fw 
= new FileWriter(path, true);
            fw.write(String.format(
"%s - %s\r\n", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .format((
new Date())), msg));
            fw.flush();
            fw.close();
        } 
catch (IOException ex) {
            utils.Log.add(
this).error(ex.getMessage());
        }
    }
}

在JSP中调用
<%
    MainJob job = new MainJob(this.getServletContext().getRealPath("/"));
    job.start();
%>




 

相关文章:

  • 2022-12-23
  • 2021-12-28
  • 2021-11-22
  • 2021-11-20
  • 2022-12-23
  • 2021-11-23
  • 2021-12-08
  • 2021-11-23
猜你喜欢
  • 2021-08-21
  • 2021-12-19
  • 2021-12-20
  • 2021-10-15
  • 2021-08-23
  • 2021-12-08
  • 2021-06-29
相关资源
相似解决方案