修改文件内容

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("操作结束");
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值