查找并替换文件中的单词/行 [英] Find and replace words/lines in a file
问题描述
我有一个文件(更具体地说,是一个log4j配置文件),我希望能够读取文件并挑出代码中的某些行并替换它们。例如,在文件中有一串文本,指示它存储的目录或记录器的级别。我希望能够在不读取文件的情况下替换这些文本字符串,将其写入另一个文件并删除原始文件。有没有更有效的方法来找到并替换使用Java文件中的文本?
下面是我正在尝试使用的文本文件的示例:
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编写的。
<?xml version =1.0encoding =UTF-8standalone = ? 无 >
<!DOCTYPE log4j:configuration SYSTEMlog4j.dtd>
< log4j:配置>
< appender class =org.apache.log4j.RollingFileAppendername =A0>
< param name =appendvalue =false/>
< param name =Filevalue =C:/log/.txt/>
< param name =MaxBackupIndexvalue =1/>
< param name =MaxFileSizevalue =100KB/>
< layout class =org.apache.log4j.PatternLayout>
< param name =ConversionPatternvalue =% - 4r [%t]%-5p:%c%x - %m%n/>
< / layout>
< / appender>
< root>
< level value =DEBUG/>
< appender-ref ref =A0/>
< / root>
< / log4j:配置>
我在想这种类型的实现可能使用哈希映射吗?
任何体面的文本编辑器都有一个支持正则表达式的搜索和替换工具。
然而,如果你有理由重新发明Java,你可以这样做:
Path path = Paths.get(test.txt);
字符集字符集= 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中,你可以这样做:
$ b $ pre $ String content = IOUtils.toString(new FileInputStream(myfile), myencoding);
content = content.replaceAll(myPattern,myReplacement);
IOUtils.write(content,new FileOutputStream(myfile),myencoding);
在这种情况下,您需要添加错误处理并在完成后关闭流他们。
IOUtils
记录在 http://commons.apache.org/proper/commons-io/javadocs/api-release /org/apache/commons/io/IOUtils.html
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
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.
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));
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
is documented at http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/IOUtils.html
这篇关于查找并替换文件中的单词/行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!