日期:2014-05-20 浏览次数:20990 次
#DBCP database properties username=root dontencry=YES password=root driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/gcra
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();
}
}
}
}
raf.seek(oldPos); 改为 raf.seek(oldPos + key.length() + 1); //+1是因为有个等号
------解决方案--------------------
如果文件完全没有改变就不是因为位置的问题
没有抛异常吗?
另外,检查下你的value变量传入的对不对