主要用于Android的values资源文件下的资源整合
一,代码
public class DuplicateFileContentDelete { public static void main(String[] args) throws DocumentException, IOException { String secretResPath = "/Users/jiajia/Downloads/app/res";//这个是保持不变,用来比较的文件夹 String testResPath = "/Users/jiajia/Downloads/res";//这个是我最后的标准,这个里面如果有跟secretResPath重复的资源可以删除,复写 File filesCompare = new File(secretResPath); //删除的资源输出 String deleteTxt = "/Users/jiajia/Downloads/detele.txt"; File files = new File(testResPath); File[] listFiles = files.listFiles(); for (File listFile : listFiles) {//res文件下的所有文件 if (listFile.isDirectory() && listFile.getName().contains("values")) {//找到所有的value文件夹 File[] childListFiles = listFile.listFiles(); for (File childListFile : childListFiles) {//找到values文件夹下的所有文件 //System.out.println(childListFile.getAbsolutePath()); //读取XML文件,获得document对象. SAXReader reader = new SAXReader(); Document document = reader.read(childListFile.getAbsolutePath()); Element rootElement = document.getRootElement();//获取文件的根结点 //System.out.println(rootElement.getName()); //对某节点下的所有子节点进行遍历. for (Iterator it = rootElement.elementIterator(); it.hasNext(); ) { Element element = (Element) it.next(); //System.out.println(element.getName());//子节点的节点名 // Iterator<Attribute> iterator = element.attributeIterator(); // for(Iterator itt = iterator;itt.hasNext();){ // //获取子节点的属性 // Attribute next = (Attribute) itt.next(); // System.out.println(next.getName() + "==" + next.getValue()); // } Attribute attribute = element.attribute("name");//获取子节点属性"name" // attribute.getValue();//节点属性"name"对应的值 // System.out.println(attribute.getName() + "==" + attribute.getValue()); for (File listFileCompare : filesCompare.listFiles()) {//res文件下的所有文件 if (listFileCompare.isDirectory() && listFileCompare.getName().contains("values")) {//找到所有的value文件夹 File[] childListFilesCompares = listFileCompare.listFiles(); for (File childListFileCompare : childListFilesCompares) {//找到values文件夹下的所有文件 //读取XML文件,获得document对象. SAXReader readerCompare = new SAXReader(); Document documentCompare = readerCompare.read(childListFileCompare.getAbsolutePath()); Element rootElementCompare = documentCompare.getRootElement();//获取文件的根结点 //对某节点下的所有子节点进行遍历. for (Iterator itCompare = rootElementCompare.elementIterator(); itCompare.hasNext(); ) { Element elementCompare = (Element) itCompare.next(); Attribute attributeCompare = elementCompare.attribute("name");//获取子节点属性"name" if (element.getName().equals(elementCompare.getName()) && attribute.getValue().equals(attributeCompare.getValue())) { // System.out.println("DELTE_ELEMENT" + childListFile.getAbsolutePath()); // System.out.println("DELTE_ELEMENT" + element.getName()); // System.out.println("DELTE_ELEMENT" + attribute.getName() + "==" + attribute.getValue()); FileOutputStream delete = new FileOutputStream(deleteTxt, true); delete.write((childListFile.getAbsolutePath() + "==" + childListFileCompare.getAbsolutePath()).getBytes()); delete.write("\r\n".getBytes()); delete.write(attribute.getValue().getBytes()); delete.write("\r\n".getBytes()); delete.write("\r\n".getBytes()); delete.close(); rootElement.remove(element);//移除test里面的元素 //将修改后的document写出覆盖原本的 //指定文件输出的位置 FileOutputStream out = new FileOutputStream(childListFile.getAbsolutePath()); // 指定文本的写出的格式: OutputFormat format = OutputFormat.createPrettyPrint(); //漂亮格式:有空格换行 format.setEncoding("UTF-8"); //1.创建写出对象 XMLWriter writer = new XMLWriter(out, format); //2.写出Document对象 writer.write(document); //3.关闭流 writer.close(); } } } } } } } } } } }
二,Android studio下的依赖添加
dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation files('/Users/jiajia/Downloads/dom4j-2.1.1.jar') }
三,jar文件
自己可以去网上下载对应dom4j-2.1.1.jar