最近在Linux上定时执行脚本程序的要求越来越多了,今天的任务是:在Linux服务器上编一个脚本,并定时执行该脚本(脚本的目的执行一个Java程序,要求java程序要打成jar包)。
1.编写java程序(公司要求我要读取一个txt文件,并将txt文件的不同的ip做一个统计在存放到一个新的txt文件中)
package com.easymorse;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
public class Test1 {
/**
* author jordy
*/
public static void main(String[] args) {
HashSet set = new HashSet();
HashSet set1 = new HashSet();
String temp = "";
boolean tf;
try {
//要读取的txt文件
FileReader fr = new FileReader("WebRoot\\resource\\welcomelog.txt");
BufferedReader in = new BufferedReader(fr);
String line;
while ((line = in.readLine()) != null){
temp = line.substring(0, 14);
tf = set.add(temp);
if(tf){
int firstleft = line.indexOf('(');
int firstright = line.indexOf(')')+1;
String othercontent = line.substring(firstleft, firstright);
String allcontent = temp + " -*-*-*-*-*-*-*-*- " + othercontent + "\n";
set1.add(allcontent);
}
}
set = null;
Iterator it = set1.iterator();
while(it.hasNext()){
String content = (String)it.next();
//新文件保存的路径
write("D:\\123.txt",content);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public static void write(String path,String content){
String s = new String();
String s1 = new String();
try{
File f = new File(path);
if(f.exists()){
System.out.println("文件存在");
}else{
System.out.println("文件不存在,正在创建...");
if(f.createNewFile()){
System.out.println("文件创建创建成功");
}else{
System.out.println("文件创建创建失败");
}
}
BufferedReader input = new BufferedReader(new FileReader(f));
while((s = input.readLine()) != null){
s1 += s + "\n";
}
System.out.println("文件以前的内容:" + s1);
input.close();
s1 += content;
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
}