2014-04-13 37 views
-1

好东西取代它,所以我想通过YAML文件搜索找到的代码如何找到在一个文本文件中的特定行,并与其他

YAML的文件通常会看起来像一个特定的行这一点,但它可能有正负其他一些线路:

timestamps: 
    login: 1397256621980 
    logout: 1397267743528 
    lastteleport: 1397265370690 
ipAddress: 127.0.0.1 
lastlocation: 
    world: world 
    x: -208.52097206207856 
    y: 64.0 
    z: -367.26689797238714 
    yaw: 199.81995 
    pitch: 11.099998 
logoutlocation: 
    world: world 
    x: -160.36469909073216 
    y: 64.0 
    z: -422.31472024960175 
    yaw: 235.05505 
    pitch: 30.90003 
money: '1.0' 
socialspy: true 
afk: false 
uuid: b735fgb3-9830-3556-bdf3-5abdd9206368 
powertools: {} 

我试图取代线

socialspy: true 

socialspy: false 

但约30分钟后,我没有运气。我了解扫描仪的基本知识,但对文件编写者等没有任何了解。

+1

好了,这听起来像你应该做的文件作家和这样的一些研究。 –

+0

不要编辑文件。将结果写入另一个文件,然后重命名为原始文件 – fge

+0

编辑文件是否是一件坏事? – user3461895

回答

0

逐行读取文件中的行,并检查它是否是不是bad string其写入到一个新的文件:

try { 
String oldFile = "oldFileNname"; 
String newFile = "newFileName"; 
PrintWriter f = new PrintWriter(new FileWriter(newFile)); 


BufferedReader br = new BufferedReader(new FileReader(oldFile)); 
String line; 
while ((line = br.readLine()) != null) { 

    if (line.equals("socialspy: true")) { 
     f.println("socialspy: false"); 
    } 
    else { 
     f.println(line); 
    } 
} 
br.close(); 
f.close(); 
}catch (IOException e) { 
    //OMG Exception again! 
} 
+0

谢谢,这个工程,但我仍然有问题,实际上取代线。这是我坚持的部分。 – user3461895

+0

不客气,我只是补充了一部分。 – mok

+0

感谢它的工作。我不得不用你给我的代码改变一件事情......但你太快关闭了PrintWriter! – user3461895

相关问题