简体   繁体   English

使用Java替换文本文件中的字符串

[英]Replace string in text file using Java

I have a text file (file.txt) which contains multiple lines: 我有一个包含多行的文本文件(file.txt):

/location/test/file.csv
/location/test1/file2.csv
/location/test2/file.exe

Using ECMA, I would like to replace all instance of "/" with "\\". 使用ECMA,我想将所有“ /”实例替换为“ \\”。 However, the code below only replaces the first line and eliminate lines 2 and 3. 但是,下面的代码仅替换第一行,并删除了第二行和第三行。

This is the result of the file.txt file after I run the code (as I said, lines 2 and 3 are missing): 这是我运行代码后file.txt文件的结果(如我所说,第2行和第3行丢失了):

\location\test\file.csv

Can anyone please help? 谁能帮忙吗?

function ReadFile ()
{
var file = new java.io.BufferedReader(new java.io.FileReader("C:\\Test\\file.txt"));
var fileWriter = new java.io.FileWriter("C:\\Test\\file.txt",false);
while ((line = file.readLine()) != null) 
 {
println(line);
if (line.contains ("/"))
line = line.replace("/","\\");

fileWriter.write(line);
fileWriter.close();
 }
} 
ReadFile ();

JavaScript String object has a replace method that takes a regular expression that can replace characters in a String. JavaScript String对象具有一个replace方法,该方法带有一个正则表达式,可以替换String中的字符。 However, the Java in your JavaScript won't work because you are mixing up two languages. 但是,JavaScript中的Java无法使用,因为您要混合两种语言。

http://www.w3schools.com/jsref/jsref_replace.asp http://www.w3schools.com/jsref/jsref_replace.asp

The problem is that you're writing over the file as you're reading it, so as soon as your first write(line) completes, the file no longer has the next two lines. 问题是您在读取文件时正在覆盖文件,因此,一旦您的第一个write(line)完成,文件将不再具有接下来的两行。 Either use a second, temporary file to write to until you've finished processing the file, or keep a list/array of all the new lines in memory, and then write out the file at the end. 使用第二个临时文件进行写入,直到完成文件处理为止,或者在内存中保留所有新行的列表/数组,然后在末尾写出文件。

I'm really curious to know, though, how you managed to get your current program to even run. 但是,我真的很好奇,您如何设法使当前程序运行。 In my experience, Java and ECMA/Javascript are two completely separate languages, but it looks like you're using javascript code against Java libraries. 以我的经验,Java和ECMA / Javascript是两种完全独立的语言,但是您似乎在针对Java库使用JavaScript代码。 What's up with that? 那是怎么回事?

So I managed to run this code using Rhino. 所以我设法使用Rhino来执行这段程式码。 It does in fact run. 它确实可以运行。 I made some changes to filenames in order to get it running on my mac, but it is essentially the same code: 为了使它在Mac上运行,我对文件名进行了一些更改,但这实际上是相同的代码:

function ReadFile () {
  var file = new java.io.BufferedReader(new java.io.FileReader("file"));
  var fileWriter = new java.io.FileWriter("file2",false);
  while ((line = file.readLine()) != null) {
    if (line.contains ("/"))
      line = line.replace("/","\\");

    fileWriter.write(line + "\n");
 }
 fileWriter.close();
 file.close();
}
ReadFile ();

So the bugs you had were: 因此,您遇到的错误是:

  1. Reading and writing to the same file. 读取和写入相同的文件。 This is awkward using streams. 使用流很尴尬。 Basically, don't do it. 基本上,不要这样做。 I changed it to write to file2. 我将其更改为写入file2。
  2. Closing the writer inside your reader loop. 在读者循环中关闭作者。 Close the writer at the end, once closed, you can no longer write to it. 最后关闭编写器,一旦关闭,就不能再写了。
  3. Not closing the file you are reading from. 不关闭正在读取的文件。

For those interested in how I got this running, on OS X, using rhino: 对于那些对我如何使用Rhino在OS X上运行它感兴趣的人:

brew install rhino
rhino example.js

I had to remove the println because that wasn't recognised but that wasn't a critical part. 我不得不删除println,因为这未被识别,但这不是关键部分。 JS on the JVM. JVM上的JS。 Fun! 好玩! Except nothing is async..... 除了没有什么是异步的.....

There are other JS engines too, but rhino worked. 也有其他JS引擎,但是rhino起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM