欲修改database.properties文件,文件内容如下:
#DBCP database properties
username=root
dontencry=YES
password=root
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/gcra
只需将password=root修改为:password=admin 。
我以为可以直接使用java.io包中的类,譬如RandomAccessFile直接write就完事了。自己做实验,设置写入点,然后调用write,但方法是调用了,文件并没有改变。   public void modifyFile(String fileName, String key, String value){
OptProperties op = new OptProperties();

File file = op.getFileFromBasePath(fileName);
RandomAccessFile raf = null;
//position
long oldPos = 0;
long newPos = 0;

String lineStr = "";
try {
raf = new RandomAccessFile(file, "rw");
oldPos = raf.getFilePointer();
lineStr = raf.readLine();
newPos = raf.getFilePointer();
while(lineStr != null){
//key = "password"
if(lineStr.contains(key)){
//将读取位置设置到oldPos
raf.seek(oldPos);
System.out.println(oldPos);
raf.write(value.getBytes());
//raf.write(value.getBytes(), (int)(oldPos+1), (int)(newPos-oldPos));
break;
}
oldPos = raf.getFilePointer();
lineStr = raf.readLine();
newPos = raf.getFilePointer();
}

/*for(int i=0; i<7; i++){
System.out.println(raf.readLine() + ", " + raf.getFilePointer());
}*/

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(raf != null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}======