欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot框架如何管理Xml和CSV

 更新時間:2021年06月16日 16:15:01   作者:知了一笑  
XML是可擴展標記語言,是一種用于標記電子文件使其具有結(jié)構(gòu)性的標記語言。CSV是一種通用的、相對簡單的文件格式,通常被用在大數(shù)據(jù)領域,進行大規(guī)模的數(shù)據(jù)搬運操作,本文將介紹SpringBoot框架如何管理Xml和CSV

一、文檔類型簡介

1、XML文檔

XML是可擴展標記語言,是一種用于標記電子文件使其具有結(jié)構(gòu)性的標記語言。標記指計算機所能理解的信息符號,通過此種標記,計算機之間可以處理包含各種的信息比如數(shù)據(jù)結(jié)構(gòu),格式等。它可以用來標記數(shù)據(jù)、定義數(shù)據(jù)類型,是一種允許用戶對自己的標記語言進行定義的源語言。適合網(wǎng)絡傳輸,提供統(tǒng)一的方法來描述和交換應用程序的結(jié)構(gòu)化數(shù)據(jù)。

2、CSV文檔

CSV文檔,以逗號分隔文檔內(nèi)容值,其文件以純文本形式存儲結(jié)構(gòu)數(shù)據(jù)。CSV文件由任意數(shù)目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號。CSV是一種通用的、相對簡單的文件格式,通常被用在大數(shù)據(jù)領域,進行大規(guī)模的數(shù)據(jù)搬運操作。

二、XML文件管理

1、Dom4j依賴

Dom4j是基于Java編寫的XML文件操作的API包,用來讀寫XML文件。具有性能優(yōu)異、功能強大和簡單易使用的特點。

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>1.1.6</version>
</dependency>

2、基于API封裝方法

涉及對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é)點
     */
    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 對象
        Document document = DocumentHelper.createDocument();
        // 創(chuàng)建節(jié)點,首個節(jié)點默認為根節(jié)點
        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");
        //設置輸出編碼
        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é)點
     */
    public static void updateXML (String filePath) throws Exception {
        Document document = getDocument (filePath) ;
        if (document != null){
            // 修改指定節(jié)點
            List elementList = document.selectNodes("/project/parent/groupId");
            Iterator iterator = elementList.iterator() ;
            while (iterator.hasNext()){
                Element element = (Element) iterator.next() ;
                element.setText("spring-boot-2");
            }
            //設置輸出編碼
            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é)點
     */
    public static void removeElement (String filePath) throws Exception {
        Document document = getDocument (filePath) ;
        if (document != null){
            // 修改指定節(jié)點
            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) ;
                    }
                }
            }
            //設置輸出編碼
            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é)點遍歷
        iteratorNode(filePath);
        // 3、創(chuàng)建XML文件
        String newFile = "F:\\file-type\\project-cf-new.xml" ;
        createXML(newFile) ;
        // 4、更新XML文件
        updateXML(newFile) ;
        // 5、刪除節(jié)點
        removeElement(newFile) ;
    }
}

3、執(zhí)行效果圖

三、CSV文件管理

1、CSV文件樣式

這里不需要依賴特定的Jar包,按照普通的文件讀取即可。

2、文件讀取

@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<DataInfo> 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);
}

3、文件創(chuàng)建

@Async
@Override
public void createCsv(List<String> 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();
}

4、編寫測試接口

這里基于Swagger2管理接口測試 。

@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<String> dataList = new ArrayList<>() ;
        dataList.add("1,北京,beijing") ;
        dataList.add("2,上海,shanghai") ;
        dataList.add("3,蘇州,suzhou") ;
        csvService.createCsv(dataList,path);
        return "success" ;
    }
}

四、源代碼地址

文中涉及文件類型,在該章節(jié)源碼ware18-file-parent/case-file-type目錄下。

GitHub·地址
https://github.com/cicadasmile/middle-ware-parent
GitEE·地址
https://gitee.com/cicadasmile/middle-ware-parent

以上就是SpringBoot框架如何管理Xml和CSV的詳細內(nèi)容,更多關于SpringBoot管理Xml和CSV的資料請關注腳本之家其它相關文章!

相關文章

  • 淺談Zookeeper開源客戶端框架Curator

    淺談Zookeeper開源客戶端框架Curator

    這篇文章主要介紹了淺談Zookeeper開源客戶端框架Curator的相關內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • IDEA如何開啟并配置services窗口

    IDEA如何開啟并配置services窗口

    在使用IntelliJ IDEA時,可能會遇到Services窗口不自動彈出的情況,本文介紹了如何手動開啟Services窗口的簡單步驟,首先,通過點擊菜單欄中的“視圖”->“工具窗口”->“服務”,或使用快捷鍵Alt+F8(注意快捷鍵可能存在沖突)來打開Services窗口
    2024-10-10
  • 后端返回各種圖片形式在前端的轉(zhuǎn)換及展示方法對比

    后端返回各種圖片形式在前端的轉(zhuǎn)換及展示方法對比

    這篇文章主要給大家介紹了關于后端返回各種圖片形式在前端的轉(zhuǎn)換及展示方法對比的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-06-06
  • 實例講述Java IO文件復制

    實例講述Java IO文件復制

    本篇文章通過實例給大家詳細講述Java IO文件復制的相關知識點,需要的讀者們學習下吧。
    2018-02-02
  • java8 filter方法、Predicate接口的使用方式

    java8 filter方法、Predicate接口的使用方式

    這篇文章主要介紹了java8 filter方法、Predicate接口的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringBoot實現(xiàn)License生成和校驗的過程詳解

    SpringBoot實現(xiàn)License生成和校驗的過程詳解

    在我們向客戶銷售商業(yè)軟件的時候,常常需要對所發(fā)布的軟件實行一系列管控措施,諸如驗證使用者身份、軟件是否到期,以及保存版權(quán)信息和開發(fā)商詳情等,所以本文給大家介紹了SpringBoot實現(xiàn)License生成和校驗的過程,需要的朋友可以參考下
    2024-09-09
  • Java異步編程的5種異步實現(xiàn)方式詳解

    Java異步編程的5種異步實現(xiàn)方式詳解

    這篇文章主要介紹了Java異步編程的5種異步實現(xiàn)方式詳解,異步編程是程序并發(fā)運行的一種手段,它允許多個事件同時發(fā)生,當程序調(diào)用需要長時間運行的方法時,它不會阻塞當前的執(zhí)行流程,程序可以繼續(xù)運行,需要的朋友可以參考下
    2024-01-01
  • SpringBoot整合SpringBoot-Admin實現(xiàn)監(jiān)控應用功能

    SpringBoot整合SpringBoot-Admin實現(xiàn)監(jiān)控應用功能

    本文主要介紹如何整合Spring Boot Admin,以此監(jiān)控Springboot應用,文中有相關的示例代碼供大家參考,需要的朋友可以參考下
    2023-05-05
  • Java MAVEN 工程pom配置報錯解決方案

    Java MAVEN 工程pom配置報錯解決方案

    這篇文章主要介紹了Java MAVEN 工程pom配置報錯解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • IDEA生成patch和使用patch的方法實現(xiàn)

    IDEA生成patch和使用patch的方法實現(xiàn)

    比如你本地修復的 bug,需要把增量文件發(fā)給客戶,很多場景下大家都需要手工整理修改的文件,并整理好目錄,這個很麻煩,那有沒有簡單的技巧呢?本文主要介紹了IDEA生成patch和使用patch的方法實現(xiàn),感興趣的可以了解一下
    2023-08-08

最新評論