最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot框架如何管理Xml和CSV

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

一、文檔類(lèi)型簡(jiǎn)介

1、XML文檔

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

2、CSV文檔

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

二、XML文件管理

1、Dom4j依賴(lài)

Dom4j是基于Java編寫(xiě)的XML文件操作的API包,用來(lái)讀寫(xiě)XML文件。具有性能優(yōu)異、功能強(qiáng)大和簡(jiǎn)單易使用的特點(diǎn)。

<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封裝方法

涉及對(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) ;
    }
}

3、執(zhí)行效果圖

三、CSV文件管理

1、CSV文件樣式

這里不需要依賴(lài)特定的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、編寫(xiě)測(cè)試接口

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

四、源代碼地址

文中涉及文件類(lèi)型,在該章節(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的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot管理Xml和CSV的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java實(shí)現(xiàn)的程序員老黃歷實(shí)例

    Java實(shí)現(xiàn)的程序員老黃歷實(shí)例

    這篇文章主要介紹了Java實(shí)現(xiàn)的程序員老黃歷實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • mvn compile報(bào)錯(cuò)“程序包c(diǎn)om.XXX不存在”

    mvn compile報(bào)錯(cuò)“程序包c(diǎn)om.XXX不存在”

    本文主要介紹了mvn compile報(bào)錯(cuò)“程序包c(diǎn)om.XXX不存在”,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Spring動(dòng)態(tài)監(jiān)聽(tīng)Nacos配置中心key值變更的實(shí)現(xiàn)方法

    Spring動(dòng)態(tài)監(jiān)聽(tīng)Nacos配置中心key值變更的實(shí)現(xiàn)方法

    Nacos本身提供支持監(jiān)聽(tīng)配置變更的操作,但在使用起來(lái),個(gè)人感覺(jué)不是很友好,無(wú)法精確到某個(gè)key的變更監(jiān)聽(tīng),所以本文小編給大家介紹了Spring動(dòng)態(tài)監(jiān)聽(tīng)Nacos配置中心key值變更的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2024-08-08
  • 詳解MyBatisPlus如何實(shí)現(xiàn)分頁(yè)和查詢(xún)操作

    詳解MyBatisPlus如何實(shí)現(xiàn)分頁(yè)和查詢(xún)操作

    這篇文章主要為大家詳細(xì)介紹了MyBatisPlus是如何實(shí)現(xiàn)分頁(yè)和查詢(xún)操作的,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的幫助,需要的可以參考一下
    2022-05-05
  • java實(shí)現(xiàn)科學(xué)計(jì)算器的全過(guò)程與代碼

    java實(shí)現(xiàn)科學(xué)計(jì)算器的全過(guò)程與代碼

    最近編寫(xiě)了一個(gè)功能較全面的科學(xué)計(jì)算器,該計(jì)算器不僅能進(jìn)行加、減、乘、除等混合運(yùn)算,而且能計(jì)算sin、cos、tan、log等函數(shù)的值,還要具有清零、退格、求倒數(shù)、求相反數(shù)等功能,這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)科學(xué)計(jì)算器的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • MyBatis Plus關(guān)閉SQL日志打印的方法

    MyBatis Plus關(guān)閉SQL日志打印的方法

    這篇文章主要介紹了MyBatis-Plus如何關(guān)閉SQL日志打印,文中通過(guò)圖文結(jié)合講解的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2024-02-02
  • java實(shí)現(xiàn)五子棋程序

    java實(shí)現(xiàn)五子棋程序

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)五子棋程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • mybatis and,or復(fù)合查詢(xún)操作

    mybatis and,or復(fù)合查詢(xún)操作

    這篇文章主要介紹了mybatis and,or復(fù)合查詢(xún)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • Java下變量大小寫(xiě)駝峰、大小寫(xiě)下劃線(xiàn)、大小寫(xiě)連線(xiàn)轉(zhuǎn)換

    Java下變量大小寫(xiě)駝峰、大小寫(xiě)下劃線(xiàn)、大小寫(xiě)連線(xiàn)轉(zhuǎn)換

    有時(shí)候需要處理對(duì)象屬性的getter、setter方法,或者將屬性與數(shù)據(jù)表字段進(jìn)行相互轉(zhuǎn)換,感興趣的可以了解一下
    2021-06-06
  • Knife4j?3.0.3?整合SpringBoot?2.6.4的詳細(xì)過(guò)程

    Knife4j?3.0.3?整合SpringBoot?2.6.4的詳細(xì)過(guò)程

    本文要講的是?Knife4j?3.0.3?整合SpringBoot?2.6.4,在SpringBoot?2.4以上的版本和之前的版本還是不一樣的,這個(gè)也容易導(dǎo)致一些問(wèn)題,本文就這兩個(gè)版本的整合做一個(gè)實(shí)戰(zhàn)介紹
    2022-09-09

最新評(píng)論

田林县| 商水县| 峨边| 龙泉市| 儋州市| 内江市| 绥德县| 六安市| 阿瓦提县| 千阳县| 武城县| 清徐县| 女性| 鄂伦春自治旗| 通州区| 伽师县| 横山县| 玉龙| 岱山县| 渑池县| 和硕县| 江口县| 北辰区| 瑞安市| 北宁市| 津市市| 察隅县| 土默特左旗| 濮阳县| 德化县| 峡江县| 朝阳区| 廊坊市| 和政县| 巴东县| 肥西县| 交城县| 通榆县| 深水埗区| 石屏县| 顺昌县|