Hi sorry for bad things.

this is a text file

[TESTRESULT]
testdate=5/16/2013
testtime=20:03:00
operator=Jacob Poulsen
test_no=62495
axles_tested=2
[AXLE1RESULT]
fric_l=38
fric_r=51
p0_l=0
p0_r=0
fl=280
fr=300

So i find issue for first find string ex:
second append text before [AXLE1RESULT]
Like this

[TESTRESULT]
testdate=5/16/2013
testtime=20:03:00
operator=Jacob Poulsen
test_no=62495
axles_tested=2
[SomeText]
Something=0
Something=0
Something=0
[AXLE1RESULT]
fric_l=38
fric_r=51
p0_l=0
p0_r=0
fl=280
fr=300

What should i do???

有帮助吗?

解决方案

This should solve your problem:

BufferedReader br= new BufferedReader(new FileReader("yourfilename.txt"));
String dataRow=null;
string tempText="";
while ( (dataRow= br.readLine()) dataRow != null){
    if(dataRow.equalsIgnoreCase("[AXLE1RESULT]"){
        tempText+="[SomeText]";
        tempText+=System.getProperty("line.separator");
        tempText+="Something=0";
        tempText+=System.getProperty("line.separator");
        tempText+="Something=0";
        tempText+=System.getProperty("line.separator");
        tempText+="Something=0";
        tempText+=System.getProperty("line.separator");
    }
    tempText+=dataRow;
    tempText+=System.getProperty("line.separator");
}

FileWriter writer = new FileWriter("yourfilename.txt");
writer.append(tempText);    
writer.flush();
writer.close();

其他提示

Better read it from one file and write it another file. If you want to append some text check for the occurrence and append it. Once you done with your process delete the original file.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top