在Java中查找并替换几个文本文件中的单词? [英] Find and replace a word in several text files with Java?

查看:59
本文介绍了在Java中查找并替换几个文本文件中的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Java在多个文本文件中查找和替换单词?

How can I find and replace a word in several text files, using Java?

这是我为单个 String ...

Here's how I do it for a single String...

public class ReplaceAll {

    public static void main(String[] args) {
        String str = "We want replace replace word from this string";  
        str = str.replaceAll("replace", "Done");
        System.out.println(str);
    }
}

推荐答案

使用 Common IO 中的 FileUtils a>:

String[] files = { "file1.txt", "file2.txt", "file3.txt" };
for (String file : files) {
    File f = new File(file);
    String content = FileUtils.readFileToString(new File("filename.txt"));
    FileUtils.writeStringToFile(f, content.replaceAll("hello", "world"));
}

这篇关于在Java中查找并替换几个文本文件中的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆