根据特定的字符串位置将字符串附加到文件

时间:2017-02-09 06:28:33

标签: java

我有黄瓜功能文件(test.feature

Feature: annotation  
Background: 
User navigates to Facebook 
Given I am on Facebook login page 

Scenario: When I enter username as "TOM" 
And I enter password as "JERRY" 
Then Login should fail 

Scenario: When I enter username as "TOM" 
And I enter password as "JERRY" 
Then Login should fail 
But Relogin option should be available

需要通过java代码添加标签(递增顺序)。比如

 Feature: annotation  
Background: 
User navigates to Facebook 
Given I am on Facebook login page 
@tag1
Scenario: When I enter username as "TOM" 
And I enter password as "JERRY" 
Then Login should fail 
@tag2
Scenario: When I enter username as "TOM" 
And I enter password as "JERRY" 
Then Login should fail 
But Relogin option should be available

标签将以递增顺序添加。是否有任何可能的方法在字符串上方添加@tag'场景:'

如何使用java code.Any解决方案。

我尝试使用以下代码。但是无法将其附加到字符串上方的位置'场景:'

 int count =0;

           BufferedReader bufferedReader = new BufferedReader(new FileReader("test.feature"));
           FileWriter writer = new FileWriter("test.feature");
           while ((line = bufferedReader.readLine()) != null) { 
                if (line.trim().startsWith("Scenario")) {
                    count++;
                    writer.write("tag"+count);

                }
           }
           //Final Dump data here 
            writer.close();        
           bufferedReader.close();

1 个答案:

答案 0 :(得分:2)

当然,您还要打印该行以及编辑添加的新行

if (line.trim().startsWith("Scenario")) {
    count++;
    writer.write("@tag"+count);
    writer.write ("\n"); 
}
writer.write (line);
writer.write ("\n");