xml与jpg一对一映射的问题,如果jpg多余那么删了,并且将xml中的filename和path属性换成jpg的文件名和全路径

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class xmlAndJpg {
    public static void main(String[] args) throws DocumentException, IOException {
        String path = "F:\\testXMLAndJpg\\";
        File file = new File(path);
        List<String> xml = new ArrayList();
        List<String> jpg = new ArrayList();
        if (file.isDirectory()){
            System.out.println("文件夹");
            String[] fileList = file.list();
            for (int i = 0; i < fileList.length;i++){
                File files = new File(path + "\\" + fileList[i]);
                if (files.getName().endsWith(".xml")){
                    xml.add(firstUtil(files.getName()));
                }
                if (files.getName().endsWith(".jpg")){
                    jpg.add(firstUtil(files.getName()));
                }
            }
        }
        Document read = null;
        for (int j = 0 ; j < jpg.size();j++)
            if (!xml.contains(jpg.get(j))) {
                File flag = new File(path  + jpg.get(j) + ".jpg");
                flag.delete();
            } else {
                File flag1 = new File(path  + jpg.get(j) + ".xml");
                SAXReader saxReader = new SAXReader();
                read = saxReader.read(flag1);
                Element rootElement = read.getRootElement();
                List<Element> elementList = rootElement.elements();
                for (Element element : elementList) {
                    if (element.getName().equals("filename")) {
                        System.out.println(element.getText());
                        element.setText(jpg.get(j));
                    }
                    if (element.getName().equals("path")) {
                        System.out.println(element.getText());
                        element.setText(path  + jpg.get(j) + ".jpg");
                    }
                }
                FileOutputStream fileOutputStream = new FileOutputStream(flag1);
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
                read.write(bufferedWriter);
                bufferedWriter.flush();
                bufferedWriter.close();
        }

    }
    public static String firstUtil(String str){
        return str.substring(0,str.indexOf("."));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值