首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

怎么将文字中的指定文字设置为段首

2012-09-05 
如何将文字中的指定文字设置为段首问题?如何从一段杂乱无章的Log文件如下:2000-01-01 00:03:44: INFO: @(#

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

问题?


如何从一段杂乱无章的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中字符串处理可以轻松解决此问题。

附代码:

小结:若单是对字符的替换,很简单,各种文字出来工具都有这种功能,但若实现上述功能,估计还得网站下工具了,之前也没搞过这个,不过竟然知道Java中对指定字符串的替换,实现这个功能就显得简单了,只要找到对应的字符串,然后在其前面加上回车换行符号“\r\n”(windows下),“\n”(linux下)就Ok了。不过我在实现过程中出了点问题,忘了加flush();写入生成的文件为空,以后记得write方法和flush方法是铁哥们,形影不离。

 

热点排行