java 文件内容中的java正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11877232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:45:17  来源:igfitidea点击:

java regular expression in a file content

java

提问by Varaprasad B

How to use regular expressions in a file content. i am having group of files, i want to search a string in all the files and replace in all the files.

如何在文件内容中使用正则表达式。我有一组文件,我想在所有文件中搜索一个字符串并替换所有文件。

can anybody help me in this? the cose is below:

有人可以帮我吗?成本如下:

package com.java.far;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReplaceAll {

    public static void main(String[] args)throws IOException {

        Runtime r=Runtime.getRuntime();
        System.out.println(r.freeMemory());

        String path="D:\JOBRELATED\FAR";
        String files;
        File folder=new File(path);
        File[] listofFiles=folder.listFiles();
        for (int i = 0; i < listofFiles.length; i++) {
            if (listofFiles[i].isFile()) {
                files=listofFiles[i].getName();
                if(files.endsWith("tex")){
                System.out.println(files);

                BufferedReader br=new BufferedReader(new FileReader("D:\JOBRELATED\FAR\"+files));
                String line;
                while((line=br.readLine())!=null){
                Pattern p=Pattern.compile("Diamond in History and Research");
                Matcher m=p.matcher(line);
                int count=0;
                while (m.find()) {
                    count++;
                    //System.out.println(m.start() +"\t"+ count);
                    System.out.println(line);
                    m.replaceAll("abc");


                }
                }

            }
            }
        }


    }
}

回答by Teh Hippo

Looks like you're on the right track; I don't know of a framework that will find & replace within a file. I have put some other tips below which you could review.

看起来你走在正确的轨道上;我不知道可以在文件中查找和替换的框架。我在下面列出了一些其他提示,您可以查看这些提示。

Your final step which you're missing is to add a OutputWriter, or similar outputter. Once you've read the file contents, check it contains the match & replaced it, you should do a boolean check whether a change was made. If so, output the file.

您缺少的最后一步是添加一个 OutputWriter 或类似的输出器。阅读文件内容后,检查它是否包含匹配项并替换它,您应该进行布尔值检查是否进行了更改。如果是,输出文件。

Other comments: 1. You won't need to do a listofFiles[i].isFile()if you're using .listFiles()2. Compile your pattern outside of the forloop for efficiency. 3. Use a dynamic for loop, might make it easier: for(final File file : listofFiles)

其他评论: 1.listofFiles[i].isFile()如果您正在使用,则不需要执行.listFiles()2. 在for循环之外编译模式以提高效率。3.使用动态for循环,可能会更容易:for(final File file : listofFiles)

Example:

例子:

   final File[] files = new File(".").listFiles();
   final Pattern pattern = Pattern.compile(".*a.*");
   for(final File file : files) {
       System.out.println(file.getName());
       final BufferedReader reader = new BufferedReader(new FileReader(file));
       final StringBuilder contents = new StringBuilder();
       while(reader.ready()) {
           contents.append(reader.readLine());
       }
       reader.close();
       final String stringContents = contents.toString();
       if(stringContents.toString().matches(".*a.*")) {
           stringContents.replaceAll("a", "b");
           final BufferedWriter writer = new BufferedWriter(new FileWriter(file));
           writer.write(stringContents);
           writer.close();
       }
   }