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();
// 隨機添加
/*for(int i=0;i<new Random().nextInt(10);i++){
String sb1=sb.substring(new Random().nextInt(sb.length()), new Random().nextInt(sb.length()))
sb=sb.substring(new Random().nextInt(sb.length()), new Random().nextInt(sb.length()));
}*/
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();
// add(path);
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("操作結束");
}
}
}
修改文件內容
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.