import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Random;
import java.util.Scanner;
public class OperationFile {
/**
*
* @功能:向指定文件夹中循环遍历的给txt文件加入一段代码
* @时间:2017年3月7日 下午4:50:58
* @param path
* @throws IOException
*/
public static void add(String path) throws IOException {
File file = new File(path);
File[] array = file.listFiles();
for (int i = 0; i < array.length; i++) {
if (array[i].isFile()) {
String fileName = array[i].getName();
String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
if (prefix.equals("txt")) {
operation(array[i].getPath());
}
} else {
add(array[i].getPath());
}
}
}
/**
*
* @功能:向指定的目录中的txt文件中查找指定的代码,并将其替换成空格
* @时间:2017年3月7日 下午4:51:26
* @param path
* @throws IOException
*/
public static void sub(String path) throws IOException {
File file = new File(path);
File[] array = file.listFiles();
for (int i = 0; i < array.length; i++) {
if (array[i].isFile()) {
String fileName = array[i].getName();
String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
if (prefix.equals("txt")) {
operation1(array[i].getPath());
}
} else {
sub(array[i].getPath());
}
}
}
/**
*
* @功能:向一个文件中加入指定的字符串
* @时间:2017年3月7日 下午4:52:28
* @param path
* @param s
* @throws IOException
*/
public static void operation(String path) throws IOException{
InputStream is=new FileInputStream(path);
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String sb="";
int flag=0;
br.mark(flag);
while(br.ready()){
br.reset();
sb+=br.readLine();
br.mark(flag);
}
br.close();
isr.close();
is.close();
OutputStream os = new FileOutputStream(path);
byte[] bytes=sb.getBytes();
os.write(bytes);
os.close();
}
/**
*
* @功能:将文件中的指定字符串转化为空格
* @时间:2017年3月7日 下午4:53:18
* @param path
* @throws IOException
*/
public static void operation1(String path) throws IOException {
InputStream is = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String sb = "";
int flag = 0;
br.mark(flag);
while (br.ready()) {
br.reset();
sb += br.readLine();
br.mark(flag);
}
br.close();
isr.close();
is.close();
OutputStream os = new FileOutputStream(path);
byte[] old = sb.getBytes();
byte[] str = "com.henu.edu".getBytes();
compareByte(old, str);
os.write(old);
os.close();
}
/**
* '
* @功能:循环遍历一个指定的数组,并与另一个指定的数组匹配,若匹配成功就替换成0
* @时间:2017年3月7日 下午4:53:52
* @param a
* @param b
*/
public static void compareByte(byte[] a, byte[] b) {
for (int i = 0; i < (a.length - b.length + 1); i++) {
int index = 0;
for (int j = i; j < (i + b.length); j++) {
if (a[j] != b[index]) {
break;
}
index++;
}
if (index == b.length) {
for (int j = i; j < (i + b.length); j++) {
a[j] = (byte) 0;
}
}
}
}
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println("请输入密码:");
String passwd = scan.nextLine();
if (passwd.equals("123456")) {
System.out.println("请输入选择输入add还是sub:");
String flag=scan.nextLine();
if(flag.equals("add")){
System.out.println("请选择输入路径:");
String path = scan.nextLine();
System.out.println("完成");
}else if(flag.equals("sub")){
System.out.println("请选择输入路径:");
String path = scan.nextLine();
sub(path);
System.out.println("完成");
}else{
System.out.println("操作结束");
}
}else{
System.out.println("操作结束");
}
}
}