如何将文字中的指定文字设置为段首

来源:互联网 发布:飞腾排版软件官方下载 编辑:程序博客网 时间:2024/05/29 02:15

问题?


如何从一段杂乱无章的Log文件如下:


2000-01-01 00:03:44: INFO: @(#)ipsec-tools 0.8-alpha20090422 (http://ipsec-tools.sourceforge.net) 2000-01-01 00:03:44: INFO: @(#)This product linked OpenSSL 0.9.8l 5 Nov 2009 (http://www.openssl.org/) 2000-01-01 00:03:44: INFO: Reading configuration from "/var/racoon.conf" 2000-01-01 00:03:44: DEBUG: call pfkey_send_register for AH 2000-01-01 00:03:44: DEBUG: call pfkey_send_register for ESP 2000-01-01 00:03:44: DEBUG: call pfkey_send_register for IPCOMP 2000-01-01 00:03:44: DEBUG: reading config file /var/racoon.conf 2000-01-01 00:03:44: DEBUG2: lifetime = 86400 2000-01-01 00:03:44: DEBUG2: lifebyte = 0 2000-01-01 00:03:44: DEBUG2: encklen=0 2000-01-01 00:03:44: DEBUG2: p:1 t:1 2000-01-01 00:03:44: DEBUG2: 3DES-CBC(5) 2000-01-01 00:03:44: DEBUG2: SHA(2) 2000-01-01 00:03:44: DEBUG2: 1024-bit MODP group(2) 2000-01-01 00:03:44: DEBUG2: pre-shared key(1) 2000-01-01 00:03:44: DEBUG2: 2000-01-01 00:03:44: DEBUG: hmac(modp1024) 2000-01-01 00:03:44: DEBUG: no check of compression algorithm; not supported in sadb message.

将log时间找出,并位于段首依次显示出来,方便查看,效果如下效果

2000-01-01 00:03:44: INFO: @(#)ipsec-tools 0.8-alpha20090422 (http://ipsec-tools.sourceforge.net) 
2000-01-01 00:03:44: INFO: @(#)This product linked OpenSSL 0.9.8l 5 Nov 2009 (http://www.openssl.org/) 
2000-01-01 00:03:44: INFO: Reading configuration from "/var/racoon.conf" 
2000-01-01 00:03:44: DEBUG: call pfkey_send_register for AH 
2000-01-01 00:03:44: DEBUG: call pfkey_send_register for ESP 
2000-01-01 00:03:44: DEBUG: call pfkey_send_register for IPCOMP 
2000-01-01 00:03:44: DEBUG: reading config file /var/racoon.conf 
2000-01-01 00:03:44: DEBUG2: lifetime = 86400 
2000-01-01 00:03:44: DEBUG2: lifebyte = 0 
2000-01-01 00:03:44: DEBUG2: encklen=0 
2000-01-01 00:03:44: DEBUG2: p:1 t:1 
2000-01-01 00:03:44: DEBUG2: 3DES-CBC(5) 
2000-01-01 00:03:44: DEBUG2: SHA(2) 
2000-01-01 00:03:44: DEBUG2: 1024-bit MODP group(2) 
2000-01-01 00:03:44: DEBUG2: pre-shared key(1) 
2000-01-01 00:03:44: DEBUG2: 
2000-01-01 00:03:44: DEBUG: hmac(modp1024) 
2000-01-01 00:03:44: DEBUG: no check of compression algorithm; not supported in sadb message. 

处理方法:
利用Java中字符串处理可以轻松解决此问题。

附代码:

public class ReplaceString{static File filein;static File fileout;static BufferedReader filereader;static FileWriter fileWriter;public static void main(String[] args){String s = "";try{filein = new File("G:/vpnLog.txt");fileout = new File("G:/result.txt");filereader = new BufferedReader(new FileReader(filein));fileWriter = new FileWriter(fileout, true);s = filereader.readLine();while (s != null){handler(s);s = filereader.readLine();}} catch (FileNotFoundException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();}}private static void handler(String stringIn){String stringOut = "";if (stringIn.contains("2000-01-01")){stringOut = stringIn.replaceAll("2000-01-01", "\r\n" + "2000-01-01");System.out.println("有");} else{stringOut = stringIn;System.out.println("没有");}try{fileWriter.write(stringOut);fileWriter.flush();System.out.println("fileWriter====写入");} catch (IOException e){e.printStackTrace();}}}
小结:若单是对字符的替换,很简单,各种文字出来工具都有这种功能,但若实现上述功能,估计还得网站下工具了,之前也没搞过这个,不过竟然知道Java中对指定字符串的替换,实现这个功能就显得简单了,只要找到对应的字符串,然后在其前面加上回车换行符号“\r\n”(windows下),“\n”(linux下)就Ok了。不过我在实现过程中出了点问题,忘了加flush();写入生成的文件为空,以后记得write方法和flush方法是铁哥们,形影不离。

	
				
		
原创粉丝点击