好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 奋斗丶小Z 于 2016-6-6 14:26 编辑
RemoveBug.rar
(521.12 KB, 下载次数: 3)
最近电脑全部的html文件被写入了一种VB脚本,昨晚用数字杀毒杀了一晚上,早上起来看,文件里原来VB脚本的位置全部变成了空白,占了不少系统的内存,自己写了个遍历文件夹获取html文件进行替换,写的不好,拿出来献献丑,用的是java写的,直接贴源代码,也打了包[Java] 纯文本查看 复制代码 package com.remove.main;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
listFile("D:\\apache-tomcat-7.0.68");
}
private void listFile(String path){
File file=new File(path);
String[] strs={"PerfLogs","MSOCache","System Volume Information","Config.Msi","Documents and Settings"};
List<String> list=Arrays.asList(strs);
if(file.exists() /*&& !file.getName().contains("-")*/ && !list.contains(file.getName())){
File[] files=file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
String fileName=pathname.getName();
if(pathname.isDirectory() || (fileName.endsWith("htm") || fileName.endsWith("html"))){
return true;
}
return false;
}
});
if(files==null){
System.out.println(file.getName());
}
if(files.length==0){
return;
}else{
for(File fi:files){
if(fi.isDirectory()){
listFile(fi.getAbsolutePath());
}else{
read(fi.getAbsolutePath());
}
}
}
}
}
public String codeString(String fileName) throws Exception{
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
bin.close();
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
public void read(String path){
BufferedReader br=null;
BufferedWriter bw=null;
StringBuilder sb=null;
try {
String code=codeString(path);
br=new BufferedReader(new InputStreamReader(new FileInputStream(path),code ));
sb=new StringBuilder();
String line=null;
while((line=br.readLine())!=null){
sb.append(line+"\r\n");
}
String value=sb.toString();
value.replace(value, "\n"+" ");
value=value.trim();
value=value.substring(0, value.indexOf("</html>")+7);
value.trim();
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
bw.write(value);
System.out.println(path);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}).start();
}
}
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|