读取文件中的带引号的(" ")中的字符串<经典篇>

这篇博客介绍如何在Java中高效地查找并处理文件中的字符串,通过自定义的matcherStr和readFileToString方法,实现对文件内容的快速操作,以提升工作效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近,公司要加密文件中字符串,如果一个个的去查找字符串,那效率可想而知。
所以,楼主自己写了一个快速查找java文件中字符串……

Eclipse控制台输出的结果:

以字符为单位读取文件内容,一次读一个字节:
public class FunctionInfo {

    public static Sting funcs = "shae;toAddFans;method2;method3;method4";

    public static Sting GFN(int flag) {
        if (funcs.length() < 3) {
            etun null;
        }
        //funcs = "classPath|methodName;classPath|methodName;classPath|methodName;";
        Sting[] funcs_s = funcs.split(";");
        //classPath|methodName
        /*Log.d("fj", funcs_s[flag]);
        // ------------------------------------------
        Sting[] info = funcs_s[flag].split("-");*/
        etun funcs_s[flag];
    }
}

文件内容转成字符串的-----package /public class FunctionInfo {public static String funcs = "share;toAddFans;method2;method3;method4";public static String GFN(int flag) {if (funcs.length() < 3) {return null;}//funcs = "classPath|methodName;classPath|methodName;classPath|methodName;";String[] funcs_s = funcs.split(";");//classPath|methodName/*Log.d("fj", funcs_s[flag]);// ------------------------------------------String[] info = funcs_s[flag].split("-");*/return funcs_s[flag];}}

字符串-----    "share;toAddFans;method2;method3;method4"
字符串-----    "classPath|methodName;classPath|methodName;classPath|methodName;"
字符串-----    ";"
字符串-----    "fj"
字符串-----    "-"

需要操作的文件内容

public class FunctionInfo {

    public static String funcs = "share;toAddFans;method2;method3;method4";

    public static String GFN(int flag) {
        if (funcs.length() < 3) {
            return null;
        }
        //funcs = "classPath|methodName;classPath|methodName;classPath|methodName;";
        String[] funcs_s = funcs.split(";");
        //classPath|methodName
        /*Log.d("fj", funcs_s[flag]);
        // ------------------------------------------
        String[] info = funcs_s[flag].split("-");*/
        return funcs_s[flag];
    }
}

工程代码:
matcherStr方法是匹配提取字符串。

/**
     * 这个方法时提取字符串,例如:文本中含有 "java/lang/String"或者Android log、println日志文本......
     * 
     * @param strContent
     */
    public static void matcherStr(String strContent) {

        //字符串为空不继续执行字符匹配
        if (strContent==null) {
            System.out.println("字符串为空");
            return;
        }
        // 正则表达式    匹配文件中的引文引号内容
        Pattern p = Pattern.compile("\"(.*?)\"");
        // Pattern p = Pattern.compile("\"(.*?)(?<![^\\\\]\\\\)\"");
        Matcher m = p.matcher(strContent);
        ArrayList<String> list = new ArrayList<String>();
        while (m.find()) {
            list.add(m.group());
        }

        if (list.size()<=0) {
            System.out.println("提示:该文件中不含英文引号的内容!!!");
        }
        else {
            for (String s : list) {
                System.out.println("字符串-----    " + s);
            }
        }
    }

readFileToString方法是将读取的file文件内容转成字符串

/**
     * 将file文件内容转成字符串
     * 
     * @param fileName
     *            文件名
     * @param charCode
     *            文件字符编码
     * @return 文件内容字符串
     */

    public static String readFileToString(String fileName, String charCode) {
        try {
            FileInputStream fis = new FileInputStream(new File(fileName));

            InputStreamReader isr = new InputStreamReader(fis, charCode);
            // 将file文件内容转成字符串
            BufferedReader bf = new BufferedReader(isr);
            String content = "";
            StringBuilder sb = new StringBuilder();
            while (content != null) {
                content = bf.readLine();
                if (content == null) {
                    break;
                }
                sb.append(content.trim());
            }
            bf.close();

            String fileStr = sb.toString();

            if (fileStr.length()==0) {
                System.out.println("文件内容为空...");
                //文件内容为空,返回空-null
                return null;
            }else {
                System.out.println("\n文件内容转成字符串的-----" + fileStr + "\n");
                //文件内容不为空,返回字符串...
                return fileStr;
            }
        } catch (Exception e1) {

            return "读取文件过程出现异常..." + e1.getMessage();
        }
    }

顺便给出读取文件的内容的方法

/**
     * 以字符为单位读取文件,常用于读文本,数字等类型的文件
     * 
     * 每次读取单个字节
     * 
     * @param fileName
     *            文件名
     * @param stringCode
     *            文件字符编码 --一般中文会有乱码出现...可以设置utf-8或者GBK
     */

    public static void readFileSingleByChars(String fileName, String charCode) {
        File file = new File(fileName);
        Reader reader = null;
        try {
            System.out.println("以字符为单位读取文件内容,一次读一个字节:");
            // 一次读一个字符
            reader = new InputStreamReader(new FileInputStream(file), charCode); // 防止中文乱码...
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                // 由于windows环境下,rn这两个字符在一起时,表示一个换行。但如果这两个字符分开显示时,会换两次行。因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。
                if (((char) tempchar) != 'r') { 
                    System.out.print((char) tempchar);
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


/**
     * 以字符为单位读取文件,常用于读文本,数字等类型的文件 每次读取多个字节
     * 
     * @param fileName
     *            文件名
     * @param charCode
     *            文件字符编码
     */

    public static void readFileMoreByChars(String fileName, String charCode) {

        File file = new File(fileName);
        Reader reader = null;

        try {
            System.out.println("以字符为单位读取文件内容,一次读多个字节:");
            // 一次读多个字符
            char[] tempchars = new char[1024];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(fileName),
                    charCode); // 防止文件乱码...
            // 读入多个字符到字符数组中,charread为一次读取字符数
            while ((charread = reader.read(tempchars)) != -1) {

                if ((charread == tempchars.length)
                        && (tempchars[tempchars.length - 1] != 'r')) {
                    System.out.print(tempchars);
                } else {
                    for (int i = 0; i < charread; i++) {
                        if (tempchars[i] == 'r') {
                            continue;
                        } else {
                            System.out.print(tempchars[i]);
                        }
                    }
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

方法调用测试类

public static void main(String[] args) {

        String fileName = "文件全路径"


        String charCode = "utf-8"; // 文件的字符编码   utf-8 或者 GBK  
        // 测试读取文件内容的操作  <单字符读取>
        FileTest.readFileSingleByChars(fileName, charCode);

        // 将文件文本内容转化位一段字符串
        String text = FileTest.readFileToString(fileName, charCode);
        matcherStr(text);
    }                       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值