Java 操作 properties 文件
复制收展Javapackage com.leixing.test;
import java.io.*;
import java.util.Properties;
/**
* @Desc
* @Author luolei
* @Date 2021/09/18 14:32
*/
public class TestProperties {
public static void main(String[] args) throws IOException {
String filepath = "D:\\jdbc2.properties";
setProperties(filepath,"jdbc.url", "jdbc:oracle:thin:@192.168.30.21:1521:orc11g", "test");
}
/**
* 操作 properties 文件
* @param filePath 文件路径
* @param key
* @param val
* @param comments 还在文件第一行加备注
* @return
*/
public static boolean setProperties(String filePath, String key, String val, String comments){
Properties properties = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
properties.load(fis);
properties.setProperty(key, val);
FileOutputStream fos = new FileOutputStream(filePath);
properties.store(fos, comments);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45