一、文檔類型簡(jiǎn)介
成都創(chuàng)新互聯(lián)長(zhǎng)期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為綿陽企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,綿陽網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
XML是可擴(kuò)展標(biāo)記語言,是一種用于標(biāo)記電子文件使其具有結(jié)構(gòu)性的標(biāo)記語言。標(biāo)記指計(jì)算機(jī)所能理解的信息符號(hào),通過此種標(biāo)記,計(jì)算機(jī)之間可以處理包含各種的信息比如數(shù)據(jù)結(jié)構(gòu),格式等。它可以用來標(biāo)記數(shù)據(jù)、定義數(shù)據(jù)類型,是一種允許用戶對(duì)自己的標(biāo)記語言進(jìn)行定義的源語言。適合網(wǎng)絡(luò)傳輸,提供統(tǒng)一的方法來描述和交換應(yīng)用程序的結(jié)構(gòu)化數(shù)據(jù)。
CSV文檔,以逗號(hào)分隔文檔內(nèi)容值,其文件以純文本形式存儲(chǔ)結(jié)構(gòu)數(shù)據(jù)。CSV文件由任意數(shù)目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號(hào)。CSV是一種通用的、相對(duì)簡(jiǎn)單的文件格式,通常被用在大數(shù)據(jù)領(lǐng)域,進(jìn)行大規(guī)模的數(shù)據(jù)搬運(yùn)操作。
Dom4j是基于Java編寫的XML文件操作的API包,用來讀寫XML文件。具有性能優(yōu)異、功能強(qiáng)大和簡(jiǎn)單易使用的特點(diǎn)。
dom4j
dom4j
1.6.1
jaxen
jaxen
1.1.6
涉及對(duì)XML文件讀取、加載、遍歷、創(chuàng)建、修改、刪除等常用方法。
public class XmlUtil {
/**
* 創(chuàng)建文檔
*/
public static Document getDocument (String filename) {
File xmlFile = new File(filename) ;
Document document = null;
if (xmlFile.exists()){
try{
SAXReader saxReader = new SAXReader();
document = saxReader.read(xmlFile);
} catch (Exception e){
e.printStackTrace();
}
}
return document ;
}
/**
* 遍歷根節(jié)點(diǎn)
*/
public static Document iteratorNode (String filename) {
Document document = getDocument(filename) ;
if (document != null) {
Element root = document.getRootElement();
Iterator iterator = root.elementIterator() ;
while (iterator.hasNext()) {
Element element = (Element) iterator.next();
System.out.println(element.getName());
}
}
return document ;
}
/**
* 創(chuàng)建XML文檔
*/
public static void createXML (String filePath) throws Exception {
// 創(chuàng)建 Document 對(duì)象
Document document = DocumentHelper.createDocument();
// 創(chuàng)建節(jié)點(diǎn),首個(gè)節(jié)點(diǎn)默認(rèn)為根節(jié)點(diǎn)
Element rootElement = document.addElement("project");
Element parentElement = rootElement.addElement("parent");
parentElement.addComment("版本描述") ;
Element groupIdElement = parentElement.addElement("groupId") ;
Element artifactIdElement = parentElement.addElement("artifactId") ;
Element versionElement = parentElement.addElement("version") ;
groupIdElement.setText("SpringBoot2");
artifactIdElement.setText("spring-boot-starters");
versionElement.setText("2.1.3.RELEASE");
//設(shè)置輸出編碼
OutputFormat format = OutputFormat.createPrettyPrint();
File xmlFile = new File(filePath);
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile),format);
writer.write(document);
writer.close();
}
/**
* 更新節(jié)點(diǎn)
*/
public static void updateXML (String filePath) throws Exception {
Document document = getDocument (filePath) ;
if (document != null){
// 修改指定節(jié)點(diǎn)
List elementList = document.selectNodes("/project/parent/groupId");
Iterator iterator = elementList.iterator() ;
while (iterator.hasNext()){
Element element = (Element) iterator.next() ;
element.setText("spring-boot-2");
}
//設(shè)置輸出編碼
OutputFormat format = OutputFormat.createPrettyPrint();
File xmlFile = new File(filePath);
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile),format);
writer.write(document);
writer.close();
}
}
/**
* 刪除節(jié)點(diǎn)
*/
public static void removeElement (String filePath) throws Exception {
Document document = getDocument (filePath) ;
if (document != null){
// 修改指定節(jié)點(diǎn)
List elementList = document.selectNodes("/project/parent");
Iterator iterator = elementList.iterator() ;
while (iterator.hasNext()){
Element parentElement = (Element) iterator.next() ;
Iterator parentIterator = parentElement.elementIterator() ;
while (parentIterator.hasNext()){
Element childElement = (Element)parentIterator.next() ;
if (childElement.getName().equals("version")) {
parentElement.remove(childElement) ;
}
}
}
//設(shè)置輸出編碼
OutputFormat format = OutputFormat.createPrettyPrint();
File xmlFile = new File(filePath);
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile),format);
writer.write(document);
writer.close();
}
}
public static void main(String[] args) throws Exception {
String filePath = "F:\\file-type\\project-cf.xml" ;
// 1、創(chuàng)建文檔
Document document = getDocument(filePath) ;
System.out.println(document.getRootElement().getName());
// 2、根節(jié)點(diǎn)遍歷
iteratorNode(filePath);
// 3、創(chuàng)建XML文件
String newFile = "F:\\file-type\\project-cf-new.xml" ;
createXML(newFile) ;
// 4、更新XML文件
updateXML(newFile) ;
// 5、刪除節(jié)點(diǎn)
removeElement(newFile) ;
}
}
這里不需要依賴特定的Jar包,按照普通的文件讀取即可。
@Async
@Override
public void readNotify(String path, Integer columnSize) throws Exception {
File file = new File(path) ;
String fileName = file.getName() ;
int lineNum = 0 ;
if (fileName.startsWith("data-")) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"GBK") ;
BufferedReader reader = new BufferedReader(isr);
List dataInfoList = new ArrayList<>(4);
String line ;
while ((line = reader.readLine()) != null) {
lineNum ++ ;
String[] dataArray = line.split(",");
if (dataArray.length == columnSize) {
String cityName = new String(dataArray[1].getBytes(),"UTF-8") ;
dataInfoList.add(new DataInfo(Integer.parseInt(dataArray[0]),cityName,dataArray[2])) ;
}
if (dataInfoList.size() >= 4){
LOGGER.info("容器數(shù)據(jù):"+dataInfoList);
dataInfoList.clear();
}
}
if (dataInfoList.size()>0){
LOGGER.info("最后數(shù)據(jù):"+dataInfoList);
}
reader.close();
}
LOGGER.info("讀取數(shù)據(jù)條數(shù):"+lineNum);
}
@Async
@Override
public void createCsv(List dataList,String path) throws Exception {
File file = new File(path) ;
boolean createFile = false ;
if (file.exists()){
boolean deleteFile = file.delete() ;
LOGGER.info("deleteFile:"+deleteFile);
}
createFile = file.createNewFile() ;
OutputStreamWriter ost = new OutputStreamWriter(new FileOutputStream(path),"UTF-8") ;
BufferedWriter out = new BufferedWriter(ost);
if (createFile){
for (String line:dataList){
if (!StringUtils.isEmpty(line)){
out.write(line);
out.newLine();
}
}
}
out.close();
}
這里基于Swagger2管理接口測(cè)試 。
@Api("Csv接口管理")
@RestController
public class CsvWeb {
@Resource
private CsvService csvService ;
@ApiOperation(value="文件讀取")
@GetMapping("/csv/readNotify")
public String readNotify (@RequestParam("path") String path,
@RequestParam("column") Integer columnSize) throws Exception {
csvService.readNotify(path,columnSize);
return "success" ;
}
@ApiOperation(value="創(chuàng)建文件")
@GetMapping("/csv/createCsv")
public String createCsv (@RequestParam("path") String path) throws Exception {
List dataList = new ArrayList<>() ;
dataList.add("1,北京,beijing") ;
dataList.add("2,上海,shanghai") ;
dataList.add("3,蘇州,suzhou") ;
csvService.createCsv(dataList,path);
return "success" ;
}
}