简体   繁体   中英

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?

Its a config file, ie x=y I want a command to edit x=y to x=y,z.

or even x=z.

If you are using that configuration format, you might want to use

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:
app.config file:

x=y

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. 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
  • 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

key2=value2

... then load them into a Properties object. 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.

Hope this helps!

rh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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