Java读取文件内容并且更新特定行

package com.welab.automation.projects.demo;

import com.welab.automation.framework.GlobalVar;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class UpdateProperties {

    public static void main(String[] args) {
        updateForHkidInProperties();
    }

    public static void updateForHkidInProperties(){
        try {

            String path = "src/main/resources/web_loans.properties";
            StringBuffer stringBuffer = new StringBuffer();
            File f = new File(path);
            FileReader bytes = new FileReader(f);
            BufferedReader chars = new BufferedReader(bytes); //字节类型转换成字符形式
            String row ="";
            while((row=chars.readLine())!=null) {
                System.out.println(row);
                if(row.contains("hkid")){
                    stringBuffer.append("hkid="+ GlobalVar.GLOBAL_VARIABLES.get("hkid")+"\n");
                }else {
                    stringBuffer.append(row+"\n");
                }
            }
            FileWriter file = new FileWriter(path,false);//true表示追加数据
            file.write(stringBuffer.toString());
            file.close();
        } catch (Exception e) {
        }
    }
}

你可能感兴趣的:(Java,java,python,开发语言)