package com.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class FileOperation {
static ArrayList<LinkedList<String>> als = new ArrayList<LinkedList<String>>();
//读取xml文件内容
public boolean analysisXML(String strPath){
SAXReader sax=new SAXReader();//创建一个SAXReader对象
File xmlFile=new File(strPath);//根据指定的路径创建file对象
if(!xmlFile.exists()){
System.out.println("没有得到xmind解压的xml文件!");
return false;
}
Document document = null;
try {
document = sax.read(xmlFile);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//获取document对象,如果文档无节点,则会抛出Exception提前结束
Element root=document.getRootElement();//获取根节点
this.getNodes(root);//从根节点开始遍历所有节点
//当只存在中心节点时,获取中心节点
if(als.size() == 0){
List<Element> node = root.elements();
for(Element e:node){
if(e.getName().equals("topic")){
List<Element> ee = e.elements();
LinkedList<String> ls = new LinkedList<>();
ls.add(ee.get(0).getTextTrim());
als.add(ls);
}
}
}
return true;
}
//获取最长链表的长度
public int getMaxNum(){
int max = 0;
for(int i = 0; i < als.size(); i++){
if(als.get(i).size() > max){
max = als.get(i).size();
}
}
return max;
}
//清理临时文件
public void cleanTemp(File file){
if(file.isDirectory()){
File[] fl = file.listFiles();
if(fl.length != 0){
for(int i = 0; i< fl.length; i++){
cleanTemp(fl[i]);
}
}
file.delete();
}else{
file.delete();
}
}
//生成excel文件
public boolean writeExcel(String sourceFilePath){
XSSFWorkbook wb= null;
File xlsxFile = new File(sourceFilePath);
if(xlsxFile.exists()){
System.out.println("该文件已存在,无法写入");
return false;
}
Sheet sheet =null;
wb = new XSSFWorkbook();
sheet = (Sheet) wb.createSheet("sheet1");
int max = getMaxNum();
Row row = sheet.createRow(0);
row.setHeight((short) 800);
for(int j = 0; j < max; j++){
sheet.setColumnWidth(j, 30*256);
if(j == 0){
row.createCell(j).setCellValue("中心主题");
}else{
row.createCell(j).setCellValue(j + "级主题");
}
}
//循环写入行数据
for (int i = 0; i < als.size(); i++) {
row = (Row) sheet.createRow(i+1);
row.setHeight((short) 500);
for(int j = 0; j < als.get(i).size(); j++){
row.createCell(j).setCellValue(als.get(i).get(j));
}
}
OutputStream stream = null;
try {
stream = new FileOutputStream(xlsxFile);
//写入数据
wb.write(stream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
//递归遍历节点
public void getNodes(Element node){
//递归遍历当前节点所有的子节点
List<Element> listElement=node.elements();
//遍历所有一级子节点
for(Element e:listElement){
List<Element> ee = e.elements();
//找到最后一个节点
if(e.getName().equals("topic") && ee.size() == 1){
LinkedList<String> ls = new LinkedList<>();
ls.add(ee.get(0).getTextTrim());
als.add(ls);
Element nodePa = e.getParent();
//回溯路径给list赋值
while(!nodePa.getName().equals("sheet")){
if(nodePa.getName().equals("topic")){
List<Element> eee = nodePa.elements();
als.get(als.size()-1).add(0, eee.get(0).getTextTrim());
}
nodePa = nodePa.getParent();
}
}
//递归遍历
this.getNodes(e);
}
}
//解压压缩文件
public void unZipFiles(String zipFile){
long startTime=System.currentTimeMillis();
try {
ZipInputStream Zin=new ZipInputStream(new FileInputStream(
zipFile));//输入源zip路径
BufferedInputStream Bin=new BufferedInputStream(Zin);
String Parent = new File(zipFile).getParent() + "\\tm"; //输出路径(文件夹目录)
File Fout=null;
ZipEntry entry;
try {
while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
Fout=new File(Parent,entry.getName());
if(!Fout.exists()){
(new File(Fout.getParent())).mkdirs();
}
FileOutputStream out=new FileOutputStream(Fout);
BufferedOutputStream Bout=new BufferedOutputStream(out);
int b;
while((b=Bin.read())!=-1){
Bout.write(b);
}
Bout.close();
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
Bin.close();
Zin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
System.out.println("耗费时间: "+(endTime-startTime)+" ms");
}
}
- 1
- 2
前往页