简体   繁体   English

在java中,如何编辑文本文件的1行?

[英]In java, how do i edit 1 line of a text file?

Ok so I know the value of the line, I dont have the line number, how would I edit only 1 line? 好的,所以我知道线的价值,我没有行号,我怎么只编辑1行?

Its a config file, ie x=y I want a command to edit x=y to x=y,z. 它是一个配置文件,即x = y我想要一个命令来编辑x = y到x = y,z。

or even x=z. 甚至x = z。

If you are using that configuration format, you might want to use 如果您使用的是该配置格式,则可能需要使用

java.util.Properties java.util.Properties

component to read/write on that file. 要读取/写入该文件的组件。

But if you just want to edit it by hand, you can just read the file line by line and match the variable you want to change. 但是如果您只是想手动编辑它,您可以逐行读取文件并匹配您想要更改的变量。

In Java you can use `Properties class: 在Java中,您可以使用`Properties类:
app.config file: app.config文件:

x=y

java: Java的:

  public void writeConfig() throws Exception {
    Properties tempProp = new Properties();
    tempProp.load(new FileInputStream("app.config")); 
    tempProp.setProperty("x", "y,z");
    tempProp.store(new FileOutputStream("app.config"), null); 

  }

One way to do it is to: 一种方法是:

  1. Read the file into memory; 将文件读入内存; eg as an array of Strings representing the lines of the file. 例如,作为表示文件行的字符串数组。
  2. Locate the String/line you want to change. 找到要更改的字符串/行。
  3. Use a regex (or whatever) to modify the String/line 使用正则表达式(或其他)来修改字符串/行
  4. Write a new version of the file from the in memory version. 从内存版本中写入新版本的文件。

There are many variations on this. 这有很多变化。 You also need to take care when you write the new version of the file to guard against losing everything if something goes wrong during the write. 在编写新版本的文件时还需要注意,以防止在写入过程中出现问题时丢失所有内容。 (Typically you write the new version to a temporary file, rename the old version out of the way (eg as a backup) and rename the new version in place of the old one.) (通常,您将新版本写入临时文件,将旧版本重命名(例如作为备份)并重命名新版本以代替旧版本。)

Unfortunately, there is no way to add or remove characters in the middle of a regular text file without rewriting a large part of the file. 不幸的是,如果不重写文件的大部分内容,就无法在常规文本文件的中间添加或删除字符。 This "problem" is not specific to Java. 这个“问题”并不特定于Java。 It is fundamental to the way that text files are modelled / represented on most mainstream operating systems. 它是在大多数主流操作系统上建模/表示文本文件的方式的基础。

Unless the new line has the exact same length as the old one, your best bet is to 除非新线的长度与旧线完全相同,否则最好的选择是

  • Open a temporary output file 打开临时输出文件
  • Read the config file, line by line 逐行读取配置文件
  • Search for your key 搜索你的密钥
  • If you can't find it, just write the line you just read to the output file 如果找不到,只需将刚刚读取的行写入输出文件即可
  • If you can find it, write the new value to the temporary file instead 如果可以找到它,请将新值写入临时文件
  • Until you hit EOF 直到你打EOF
  • Delete old file 删除旧文件
  • Rename new file to the old file 将新文件重命名为旧文件

IF your config file is small, you can also do the whole parsing/modification step in memory and then write the final result back to the config file, that way you skip the temporary file (although a temporary file is a good way to prevent corruption if something breaks while you write the file). 如果您的配置文件很小,您还可以在内存中执行整个解析/修改步骤,然后将最终结果写回配置文件,这样就可以跳过临时文件(尽管临时文件是防止损坏的好方法如果你在写文件时出现问题)。

If this is not what you're looking for, you should edit your question to be a lot more clear. 如果这不是您想要的,那么您应该更清楚地编辑您的问题。 I'm just guessing what you're asking for. 我只想猜你要的是什么。

If your data is all key and value pairs, for example ... 如果您的数据是所有键和值对,例如......

key1=value1 键1 =值

key2=value2 键2 =值

... then load them into a Properties object. ...然后将它们加载到Properties对象中。 Off the top of my head, you'll need a FileInputStream to load the properties, modify with myProperties.put(key, value) and then save the properties with the use of a FileOutputStream. 在我的脑海中,您需要一个FileInputStream来加载属性,使用myProperties.put(key,value)进行修改,然后使用FileOutputStream保存属性。

Hope this helps! 希望这可以帮助!

rh RH

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

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