Java是编辑文本文件的最佳和最快方法。

huangapple go评论75阅读模式
英文:

Java the best and fastest way to edit text file

问题

private boolean edit(File source) {
	if (!source.getAbsolutePath().endsWith(".java")) // Java text files only
        return false;

	String l, str = "", orig = "";
	try {
		BufferedReader r = new BufferedReader(new FileReader(source));
		while ((l = r.readLine()) != null) {
			orig = str += l + "\n";
		}
		r.close();

		for (Entry<String, String> e : mappings.entrySet()) // Replacing string by HashMap mappings!
			str = fastReplace(str, e.getKey(), e.getValue()); // Faster alternative to String#replaceAll

		if (!str.equals(orig)) {
			BufferedWriter bf = new BufferedWriter(new FileWriter(source));
			bf.write(str);	
			
			bf.close();
			return true;
		}
	} catch (Exception e) {
		doLog(e.toString()); // Logging exception but unimportant for us...
	}
	return false;
}

在我找到的解决方案中,我觉得这个函数有点繁琐,因为它首先需要将文本文件读入字符串,然后进行编辑,然后再写回文件。所以问题是,是否有更好和更快的方法来编辑文本文件?我的意思是,例如是否有一种直接将文件作为文本文件进行编辑的方式,或者在不覆盖文件相同未更改部分的情况下进行写入,或者是否有更快的读写文件的方法?或者我的函数实际上已经是最快的了吗?

如果有人想知道我的 "fastReplace" 函数是做什么的,请查看这个链接:[https://stackoverflow.com/questions/1010928/faster-alternatives-to-replace-method-in-a-java-string][1],但我认为这不是重要的。


<details>
<summary>英文:</summary>

&lt;br&gt;
I need to edit a lot of text files effectively and fast! What is the best thing I can do?
I already come up with this function:

private boolean edit(File source)
{
if (!source.getAbsolutePath().endsWith(".java")) //Java text files only
return false;

String l, str = &quot;&quot;, orig = &quot;&quot;; 
try
{
	BufferedReader r = new BufferedReader(new FileReader(source));
	while ((l = r.readLine()) != null)
	{
		orig = str += l+&quot;\n&quot;;
	}
	r.close();

	for (Entry&lt;String, String&gt; e : mappings.entrySet()) //Replacing string by HashMap mappings!
		str = fastReplace(str, e.getKey(), e.getValue()); //Faster alterntive to String#replaceAll
	
	if (!str.equals(orig))
	{	
		BufferedWriter bf = new BufferedWriter(new FileWriter(source));
		bf.write(str);	
		
		bf.close();
		return true;
	}
}
catch (Exception e) 
{
	doLog(e.toString()); //Logging exception but unimportant for us...
}
return false;

}

I found my function a little bit clumsy because it first needs to read text file into string then edit it and write it back after that. So the question is. Is there any better and faster way to edit text file? I mean for example without necessity to turning it into string and then writing it back. For example is there a way to edit file directly as a text file or writing it without overriding the same unchanged parts of the file or any faster way to read and write file? Or is my function actually already fastest as it can be? &lt;br&gt;
In case somebody is wondering what my &quot;fastReplace&quot; function does then check this [https://stackoverflow.com/questions/1010928/faster-alternatives-to-replace-method-in-a-java-string][1] but I do not think it is important.


  [1]: https://stackoverflow.com/questions/1010928/faster-alternatives-to-replace-method-in-a-java-string

</details>


# 答案1
**得分**: 1

如果您需要逐字节替换为另一个完全相同大小的字符串,那么您可以按块大小顺序逐块读取数据,替换所需位置,如果已进行更改,则写回数据。如果没有进行更改,则无需写回该块。在最理想的情况下,您将节省少量I/O操作,但代价是代码复杂性较高。

如果您的编辑更复杂,并涉及字符串插入,那么您无法避免读取并完整写回整个文本。

过早优化是不明智的。源代码文件几乎不跨越单个块,在您的情况下,您可能既不会节省时间也不会节省空间。

<details>
<summary>英文:</summary>

If you need to replace a string by another one of the exact same size byte for byte, then you could read the data sequentially in chunks with multiple block size, replace the desired spots and write the data back if a change has been made. If no change was made then there is no need to write back the block. In the best scenario, you will save few I/O operations, at the cost of significant code complexity.

If your editing is more complex and involves string insertions, then you have no way out of reading and writing back the entire text.

Early optimization is a bad idea. Source code files hardly span a single block and in your case you will probably save no time or space.

</details>



huangapple
  • 本文由 发表于 2020年8月30日 01:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63650160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定