java io操作 掃描本地檔案並刪除其中某類檔案的部分內容

來源:互聯網
上載者:User

標籤:throw   hash   writer   too   hashset   post   cmem   檔案的   lis   

主要是前端時間電腦中了vbs病毒,雖然殺毒了,但是對於html檔案,仍然存在大量新增內容

<SCRIPT Language=VBScript><!--
DropFileName = "svchost.exe"
WriteData = "4D5A90000300000004000000FFFF0000B8000000000000004000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000......後面省略

因此寫了個掃描目標目錄下所有html檔案,並以<SCRIPT Language=VBScript><!--
DropFileName = "svchost.exe"為標誌,對文本進行處理,刪除html檔案中上面這段代碼的一段代碼。

直接貼代碼吧(對於c盤下部分無許可權訪問的目錄,是不能訪問的,不過這類檔案一般也不會存在被感染的html檔案)
package gwDevelop;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;

import org.apache.catalina.tribes.membership.StaticMember;

public class Tool_deletetxt {


public static void main(String[] args) {
String filepath="D://建立檔案夾";//讀取D盤
String fileEndWith=".html";//讀取其中的.html檔案
Set<String> fileSet = new HashSet<>();//儲存html檔案清單
String out="";
try {
readfile(filepath,fileEndWith,fileSet); //讀取D下的html檔案,將路徑放入fileSet中
for (String string : fileSet) {
out=changeCharacter(string,"////","////////");//轉換目錄字元串形式,將/轉化成//
try {
String a = fileRead(out); //讀取檔案out的內容,將其儲存在a字串中
String[] ppp = a.split("<SCRIPT Language=VBScript>");//根據這個標誌判斷含有vbs感染的html檔案,並截取前面正常檔案的內容
clearInfoForFile(out); //清空檔案內容
WriteStringToFile2(out,ppp[0]); //將前面正常的檔案內容重新寫入檔案
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//讀取某個檔案夾下的所有html檔案
public static boolean readfile(String filepath,String fileEndWith,Set<String> set) throws FileNotFoundException, IOException {

try {
File file = new File(filepath);
if (!file.isDirectory()&&file.getAbsolutePath().endsWith(fileEndWith)) {
set.add(file.getAbsolutePath());

} else if (file.isDirectory()) {
String[] filelist = file.list();
if (null == filelist) {
System.out.println(file.getPath()+"目錄下檔案讀取失敗");
return true;
}
if (filelist.length>0) {
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory() && readfile.getAbsolutePath().endsWith(fileEndWith)) {
set.add(readfile.getAbsolutePath());
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i],fileEndWith,set);
}
}
}
}
} catch (FileNotFoundException e) {
System.out.println(filepath);
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}

/**
* 字串處理,將/改為\\,讀取目錄使用
**/
public static String changeCharacter(String inString,String a,String b){
String outString;
outString = inString.replaceAll(a, b);
return outString;
}

//讀取檔案內容
public static String fileRead(String string) throws Exception {
FileInputStream file = new FileInputStream(string);//定義一個file對象,用來初始化FileReader
InputStreamReader reader = new InputStreamReader(file,"UTF-8");//定義一個fileReader對象,用來初始化BufferedReader
BufferedReader bReader = new BufferedReader(reader);//new一個BufferedReader對象,將檔案內容讀取到緩衝
StringBuilder sb = new StringBuilder();//定義一個字串緩衝,將字串存放緩衝中
String s = "";
while ((s =bReader.readLine()) != null) {//逐行讀取檔案內容,不讀取分行符號和末尾的空格
sb.append(s + "\n");//將讀取的字串添加分行符號後累加存放在緩衝中
}
bReader.close();
String str = sb.toString();
return str;
}

// 清空已有的檔案內容
public static void clearInfoForFile(String fileName) {
File file =new File(fileName);
System.out.println("清空檔案:"+fileName);
try {
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter =new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}

//寫入正常檔案內容
public static void WriteStringToFile2(String filePath,String content) {
try {
System.out.println("重新寫入檔案:"+filePath);
FileOutputStream file = new FileOutputStream(filePath);
OutputStreamWriter writer = new OutputStreamWriter(file, "UTF-8");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(content);// 往已有的檔案上添加字串
bw.close();
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

java io操作 掃描本地檔案並刪除其中某類檔案的部分內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.