求代码:java文件操作

来源:百度知道 编辑:UC知道 时间:2024/05/26 01:47:02
请教,求Java代码:
逐行读入一个文件xxx.xrdml,
找到一行 <startPosition>26.5462</startPosition>(两个<startPosition>中间一个数字),把数字取出除以二, 然后放回原文件, 文件另存为xxx_2.xrdml 谢谢!
希望是完整的代码, 可以直接编译,最好给点注释, 让我这个菜鸟能够看懂.
谢谢,100分送上!
唉, 兄弟们帮忙写个吧. 编译出来了追加100分.

这个问题不是很难,代码不长,如果有不懂的,可以发消息过来:

import java.io.*;

public class ReadTest {
public static void main(String[] args) throws Exception {
File f=new File("d:\\xxx.xrdml");
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f.getParent()+f.getName().replace(".","_2."))));
String tmp=new String();
while((tmp=br.readLine())!=null){
if(tmp.contains("<startPosition>") || tmp.contains("</startPosition>")){
double num=Double.parseDouble(tmp.substring(tmp.indexOf("<startPosition>")+"<startPosition>".length(),tmp.indexOf("</startPosition>")));
num=num/2;
tmp=tmp.substring(0,tmp.indexOf("<st