查找和替换文件中的单词/行 [英] Find and replace words/lines in a file

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

问题描述

我有一个文件(更具体地说,是一个 log4j 配置文件),我希望能够读入该文件并挑选出代码中的某些行并替换它们.例如,在文件中有一串文本指示它的存储目录或记录器的级别.我希望能够在不读取文件、将其写入另一个文件和删除原始文件的情况下替换这些文本字符串.有没有更有效的方法来使用 Java 查找和替换文件中的文本?

I have a file (more specifically, a log4j configuration file) and I want to be able to read in the file and pick out certain lines in the code and replace them. For example, within the file there is a string of text that indicates the directory it is stored in, or the level of the logger. I want to be able to replace those string of text without reading in the file, writing it to another file, and deleting the original file. Is there a more efficient way of doing find and replace texts in a file using Java?

这是我尝试使用的文本文件的示例:

Here is an example of the text file I'm trying to work with:

log4j.rootLogger=DEBUG, A0

log4j.appender.A0=org.apache.log4j.RollingFileAppender
log4j.appender.A0.File=C:/log.txt
log4j.appender.A0.MaxFileSize=100KB
log4j.appender.A0.MaxBackupIndex=1

log4j.appender.A0.layout=org.apache.log4j.RollingFileAppender
log4j.appender.A0.layout.ConversionPattern=%-4r [%t] %-5p: %c %x - %m%n

我希望能够读取文件并将DEBUG"替换为另一个级别或替换文件目录名C:/log.txt".日志配置文件也是用xml写的.下面是一个例子.

I want to be able to read the file and replace 'DEBUG' with another level or replace the file directory name 'C:/log.txt'. The log configuration file is also written in xml. An example of that is featured below.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
    <appender class="org.apache.log4j.RollingFileAppender" name="A0">
        <param name="append" value="false"/>
        <param name="File" value="C:/log/.txt"/>
        <param name="MaxBackupIndex" value="1"/>
        <param name="MaxFileSize" value="100KB"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r [%t] %-5p: %c %x - %m%n"/>
        </layout>
    </appender>
    <root>
        <level value="DEBUG"/>
        <appender-ref ref="A0"/>
    </root>
</log4j:configuration>

我在想是否可以将哈希映射用于这种类型的实现?

I'm thinking it may be possible to use a hash map for this type of implementation?

推荐答案

任何体面的文本编辑器都有支持正则表达式的搜索和替换功能.

Any decent text editor has a search&replace facility that supports regular expressions.

但是,如果您有理由在 Java 中重新发明轮子,您可以这样做:

If however, you have reason to reinvent the wheel in Java, you can do:

Path path = Paths.get("test.txt");
Charset charset = StandardCharsets.UTF_8;

String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll("foo", "bar");
Files.write(path, content.getBytes(charset));

这仅适用于 Java 7 或更新版本.如果您坚持使用较旧的 Java,您可以这样做:

This only works for Java 7 or newer. If you are stuck on an older Java, you can do:

String content = IOUtils.toString(new FileInputStream(myfile), myencoding);
content = content.replaceAll(myPattern, myReplacement);
IOUtils.write(content, new FileOutputStream(myfile), myencoding);

在这种情况下,您需要添加错误处理并在完成后关闭流.

In this case, you'll need to add error handling and close the streams after you are done with them.

IOUtils 记录在 http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/IOUtils.html

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

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