Java IO Operations Scan local files and delete portions of some of these files

Source: Internet
Author: User
Tags readfile

Mainly the front-end time computer in the VBS virus, although anti-virus, but for the HTML file, there are still a lot of new content

<script language=vbscript><!--
Dropfilename = "Svchost.exe"
WriteData = " 4d5a90000300000004000000ffff0000b8000000000000004000000000000000000000000000000000000000000000000000000000000000000000000 0010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000 ... omit after

So I wrote a scan of all the HTML files in the target directory and <script language=vbscript><!--
Dropfilename = "Svchost.exe" as a flag, the text is processed, delete the HTML file in the above Code section of code.

Direct Code Bar (for the C-drive section of the directory without access to the access, but this type of file is generally not the existence of infected HTML files)
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://new Folder";//Read D-drive
String fileendwith= ". html";//Read the. html file
set<string> fileset = new hashset<> ();//Save HTML file list
String out= "";
try {
ReadFile (Filepath,fileendwith,fileset); Read the HTML file under D and put the path into Fileset
for (String String:fileset) {
Out=changecharacter (String, "////", "////////");//Convert directory string form, translate/convert//
try {
String a = FileRead (out); Reads the contents of the file out and saves it in a string
string[] PPP = A.split ("<script language=vbscript>");//The HTML file containing the VBS infection is judged by this flag and the contents of the previous normal file are intercepted
Clearinfoforfile (out); Empty file contents
WriteStringToFile2 (Out,ppp[0]); Re-write the previous normal file contents to the file
} catch (Exception e) {
E.printstacktrace ();
}
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Read all HTML files under a folder
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 () + "file read failed in directory");
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;
}

/**
* String processing, will/change \ \, read directory use
**/
public static string Changecharacter (string instring,string a,string b) {
String outstring;
outstring = Instring.replaceall (A, b);
return outstring;
}

Read File contents
public static string FileRead (String string) throws Exception {
FileInputStream file = new FileInputStream (string);//define a file object to initialize the FileReader
InputStreamReader reader = new InputStreamReader (file, "UTF-8");//define a FileReader object to initialize the BufferedReader
BufferedReader breader = new BufferedReader (reader),//new a BufferedReader object, reads the contents of the file into the cache
StringBuilder sb = new StringBuilder ();//define a string cache and store the string in the cache
String s = "";
while ((S =breader.readline ()) = null) {//Line reads the contents of the file, does not read the line break and the trailing space
Sb.append (s + "\ n");//Adds a newline character to the read string and stores it in the cache
}
Breader.close ();
String str = sb.tostring ();
return str;
}

Empty existing file contents
public static void Clearinfoforfile (String fileName) {
File file =new file (fileName);
System.out.println ("Empty file:" +filename);
try {
if (!file.exists ()) {
File.createnewfile ();
}
FileWriter FileWriter =new FileWriter (file);
Filewriter.write ("");
Filewriter.flush ();
Filewriter.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

Write Normal file contents
public static void WriteStringToFile2 (String filepath,string content) {
try {
System.out.println ("Re-write File:" +filepath);
FileOutputStream file = new FileOutputStream (FilePath);
OutputStreamWriter writer = new OutputStreamWriter (file, "UTF-8");
BufferedWriter bw = new BufferedWriter (writer);
Bw.write (content);//Add a string to an existing file
Bw.close ();
Writer.close ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

Java IO Operations Scan local files and delete portions of some of these files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.