File system (02): Based on spring boot framework, manage XML and CSV file types

Time:2020-2-26

Source code: GitHub, click here, gitee, click here

1、 Introduction to document types

1. XML document

XML is an extensible markup language, which is used to mark electronic documents and make them structural. Mark refers to the information symbol that can be understood by the computer. Through this kind of mark, the computer can process and contain various information such as data structure, format, etc. It can be used to tag data and define data types. It is a source language that allows users to define their own markup language. It is suitable for network transmission and provides a unified method to describe and exchange structured data of application programs.

2. CSV document

CSV documents, which separate document content values with commas, and whose files store structural data in plain text. The CSV file consists of any number of records separated by a line break character. Each record consists of fields. The separator between fields is other characters or strings, the most common is comma. CSV is a general and relatively simple file format, which is usually used in the field of big data for large-scale data handling operations.

2、 XML file management

1. Dom4j dependency

Dom4j is a Java based API package for XML file operations, which is used to read and write XML files. It has the characteristics of excellent performance, powerful function and easy to use.

<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 based encapsulation method

It involves the common methods of reading, loading, traversing, creating, modifying and deleting XML files.

public class XmlUtil {
    /**
     *Create document
     */
    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 ;
    }

    /**
     *Traverse root node
     */
    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 ;
    }

    /**
     *Create XML document
     */
    public static void createXML (String filePath) throws Exception {
        //Create document object
        Document document = DocumentHelper.createDocument();
        //Create a node, the first node defaults to the root node
        Element rootElement = document.addElement("project");
        Element parentElement = rootElement.addElement("parent");
        Parentelement.addcomment ("Version Description");
        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");
        //Set output code
        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();
    }

    /**
     *Update node
     */
    public static void updateXML (String filePath) throws Exception {
        Document document = getDocument (filePath) ;
        if (document != null){
            //Modify the specified node
            List elementList = document.selectNodes("/project/parent/groupId");
            Iterator iterator = elementList.iterator() ;
            while (iterator.hasNext()){
                Element element = (Element) iterator.next() ;
                element.setText("spring-boot-2");
            }
            //Set output code
            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();
        }
    }

    /**
     *Delete node
     */
    public static void removeElement (String filePath) throws Exception {
        Document document = getDocument (filePath) ;
        if (document != null){
            //Modify the specified node
            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) ;
                    }
                }
            }
            //Set output code
            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. Create document
        Document document = getDocument(filePath) ;
        System.out.println(document.getRootElement().getName());
        //2. Root node traversal
        iteratorNode(filePath);
        //3. Create XML file
        String newFile = "F:\file-type\project-cf-new.xml" ;
        createXML(newFile) ;
        //4. Update XML file
        updateXML(newFile) ;
        //5. Delete node
        removeElement(newFile) ;
    }
}

3. Executive renderings

File system (02): Based on spring boot framework, manage XML and CSV file types

3、 CSV file management

1. CSV file style

File system (02): Based on spring boot framework, manage XML and CSV file types

There is no need to rely on a specific jar package, just read it according to a normal file.

2. File read

@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 ("container data:" + datainfolist);
                dataInfoList.clear();
            }
        }
        if (dataInfoList.size()>0){
            Logger.info ("last data:" + datainfolist);
        }
        reader.close();
    }
    Logger.info ("number of data read:" + linenum);
}

3. File creation

@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. Write test interface

This is based on swagger2 management interface testing.

@API ("CSV interface management")
@RestController
public class CsvWeb {
    @Resource
    private CsvService csvService ;
    @Apioperation (value = "file read")
    @GetMapping("/csv/readNotify")
    public String readNotify (@RequestParam("path") String path,
                              @RequestParam("column") Integer columnSize) throws Exception {
        csvService.readNotify(path,columnSize);
        return "success" ;
    }
    @Apioperation (value = "create file")
    @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" ;
    }
}

4、 Source code address

Document types are involved in this article, and the source code of this chapter isware18-file-parent/case-file-typeUnder the directory.

GitHub · address
https://github.com/cicadasmile/middle-ware-parent
Gitee · address
https://gitee.com/cicadasmile/middle-ware-parent

File system (02): Based on spring boot framework, manage XML and CSV file types

Recommended reading: Spring Framework Foundation

Summary of core components, building of basic environment

Assembly, scope, life cycle of bean

Summary of core idea IOC container, case demonstration

AOP programming concept, demonstration of several implementation methods

Transaction management mechanism and implementation method

Brief introduction of MVC architecture mode and detailed explanation of implementation process

Recommended Today

Laravel service container must know

The article was forwarded from the professional laravel developer community. Original link: https://learnku.com/laravel/t To learn how to build an application with laravel is not only to learn how to use different classes and components in the framework, but also to remember allartisanCommand or all helper functions (we have Google). Learning to code with laravel is […]