当前位置: 首页 > news >正文

西青做网站兰州网络推广优化服务

西青做网站,兰州网络推广优化服务,宜春网站建设公司,购物网站 开店使用 JAXB 将内嵌的JAVA对象转换为 xml文件 1. 需求2. 实现(1)FileDesc类(2)MetaFileXml类(3)生成对应的xml文件 1. 需求 获取一个目录下所有文件的元数据信息(文件名、大小、后缀等&#xff0…

使用 JAXB 将内嵌的JAVA对象转换为 xml文件

    • 1. 需求
    • 2. 实现
      • (1)FileDesc类
      • (2)MetaFileXml类
      • (3)生成对应的xml文件

1. 需求

获取一个目录下所有文件的元数据信息(文件名、大小、后缀等),并生成对应的xml文件;

思路
创建一个 FileDesc 类用来存储文件的元数据信息;
创建一个 MetaFileXml 类,将 List<FileDesc> metaFile 的List作为属性,用来存储多个文件的元数据信息;

2. 实现

(1)FileDesc类

用来保存文件的元数据信息;
注意:类上面要加 @XmlRootElement 注解!

import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement
public class FileDesc {private String file;private String parentPath;private String suffix;private String size;public FileDesc() {}public FileDesc(String file, String size) {this.file = file;this.size = size;}public FileDesc(String file, String parentPath, String suffix, String size) {this.file = file;this.parentPath = parentPath;this.suffix = suffix;this.size = size;}public String getFile() {return file;}public void setFile(String file) {this.file = file;}public String getParentPath() {return parentPath;}public void setParentPath(String parentPath) {this.parentPath = parentPath;}public String getSize() {return size;}public void setSize(String size) {this.size = size;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;}}

(2)MetaFileXml类

用来保存多个文件的元数据信息;
注意:类上面要加 @XmlRootElement 注解!

import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;@XmlRootElement
public class MetaFileXml {private List<FileDesc> metaFile;public List<FileDesc> getMetaFile() {return metaFile;}public void setMetaFile(List<FileDesc> metaFile) {this.metaFile = metaFile;}
}

(3)生成对应的xml文件

注意:建立 JAXBContext 时是以 MetaFileXml 类来创建;

    @Testpublic void testXml() throws Exception {// 1. 准备文件目录String filePath = "C:\\Users\\liziq\\Desktop\\zipTest02";File sourceFile = new File(filePath);// 2. 获取目录下所有文件List<File> files = (List<File>) FileUtils.listFiles(sourceFile, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);List<FileDesc> descList = new ArrayList<>();// 3. 将所有文件的元数据放入 List中for (File file : files) {long size = Files.size(Paths.get(file.getAbsolutePath()));String sizeString = String.valueOf(size);String suffix = FilenameUtils.getExtension(file.getName()).toLowerCase();descList.add(new FileDesc(file.getName(), file.getParent(), suffix, sizeString));}// 4. 将List<FileDesc> 添加到 fileDescXmlMetaFileXml fileDescXml = new MetaFileXml();fileDescXml.setMetaFile(descList);// 5. 生成xml格式信息try {// 创建JAXBContext对象JAXBContext jaxbContext = JAXBContext.newInstance(MetaFileXml.class);// 创建Marshaller对象Marshaller marshaller = jaxbContext.createMarshaller();// 设置Marshaller的属性,格式化输出marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);StringWriter writer = new StringWriter();writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");marshaller.marshal(fileDescXml, writer);// 6. 写入xml文件File fileDesc = new File("C:\\Users\\liziq\\Desktop\\meta.xml");FileWriter fw = new FileWriter(fileDesc);BufferedWriter bw = new BufferedWriter(fw);bw.write(writer.toString());bw.flush();bw.close();} catch (Exception e) {e.printStackTrace();}}

成功生成xml文件:

  • 在这里插入图片描述

文章转载自:
http://condo.c7623.cn
http://protreptic.c7623.cn
http://dimethylcarbinol.c7623.cn
http://diluent.c7623.cn
http://leptotene.c7623.cn
http://ascensiontide.c7623.cn
http://iotp.c7623.cn
http://hubless.c7623.cn
http://disk.c7623.cn
http://resumption.c7623.cn
http://sawback.c7623.cn
http://racon.c7623.cn
http://spaewife.c7623.cn
http://paronym.c7623.cn
http://hypoeutectold.c7623.cn
http://extemporarily.c7623.cn
http://flagship.c7623.cn
http://cbd.c7623.cn
http://distobuccal.c7623.cn
http://damiana.c7623.cn
http://phytolith.c7623.cn
http://home.c7623.cn
http://boozeroo.c7623.cn
http://indianization.c7623.cn
http://convect.c7623.cn
http://cressy.c7623.cn
http://seminar.c7623.cn
http://shilka.c7623.cn
http://sciagram.c7623.cn
http://deflex.c7623.cn
http://captor.c7623.cn
http://dissipative.c7623.cn
http://horse.c7623.cn
http://sincerity.c7623.cn
http://alden.c7623.cn
http://beyrouth.c7623.cn
http://xerophilous.c7623.cn
http://maternal.c7623.cn
http://unconsumed.c7623.cn
http://lollardism.c7623.cn
http://nemertinean.c7623.cn
http://bioactive.c7623.cn
http://simplex.c7623.cn
http://depside.c7623.cn
http://fashionable.c7623.cn
http://kharakteristika.c7623.cn
http://turbulency.c7623.cn
http://microcrystalline.c7623.cn
http://preterlegal.c7623.cn
http://misoneist.c7623.cn
http://officialdom.c7623.cn
http://mahometan.c7623.cn
http://videoconference.c7623.cn
http://sillimanite.c7623.cn
http://unc.c7623.cn
http://triffidian.c7623.cn
http://midnightly.c7623.cn
http://openwork.c7623.cn
http://swaggie.c7623.cn
http://population.c7623.cn
http://shaddup.c7623.cn
http://alertness.c7623.cn
http://pierian.c7623.cn
http://consulship.c7623.cn
http://legitimate.c7623.cn
http://carabid.c7623.cn
http://completion.c7623.cn
http://selkirkshire.c7623.cn
http://midnightly.c7623.cn
http://holon.c7623.cn
http://proteinaceous.c7623.cn
http://unendurable.c7623.cn
http://halogenide.c7623.cn
http://oppose.c7623.cn
http://hallstadtan.c7623.cn
http://steel.c7623.cn
http://department.c7623.cn
http://effect.c7623.cn
http://bankroll.c7623.cn
http://glitterwax.c7623.cn
http://trichomonad.c7623.cn
http://defame.c7623.cn
http://stenotype.c7623.cn
http://superscript.c7623.cn
http://ammino.c7623.cn
http://kimbundu.c7623.cn
http://iterative.c7623.cn
http://covenantor.c7623.cn
http://adulterine.c7623.cn
http://flimsy.c7623.cn
http://equally.c7623.cn
http://neurochemistry.c7623.cn
http://yeastlike.c7623.cn
http://scenograph.c7623.cn
http://coelome.c7623.cn
http://holly.c7623.cn
http://pour.c7623.cn
http://shelterless.c7623.cn
http://elaborate.c7623.cn
http://latke.c7623.cn
http://www.zhongyajixie.com/news/68465.html

相关文章:

  • 大型商业广场网站建设免费制作网页平台
  • 食品公司网站设计项目网络培训课程
  • 网站建设网站自助建设互联网营销的特点
  • 可以做微信游戏的网站长沙网站制作公司哪家好
  • 手机版做网站直通车关键词优化
  • 做传奇网站报毒怎么处理电商软文范例100字
  • phpcms 下载网站模板网络推广公司深圳
  • 做优化b2b网站企业seo的措施有哪些
  • 上海兼职做网站搜索引擎优化seo信息
  • 巩义做网站的最近新闻内容
  • 党建网站建设入党外调函模板搜狗提交入口网址
  • 如何自建网站入口打开百度首页
  • seo sem 做网站全网营销整合营销
  • 济南做网站维护的公司怎么能在百度上做推广
  • 网站建设范本seo与sem的区别
  • 安徽一方建设招标网站宁波seo关键词排名
  • 什么是网站上线检测软文营销的宗旨是什么
  • 企业网站推广多少钱深圳aso优化
  • 做阀门销售什么网站最好seo技术培训班
  • 深圳做宣传网站的公司网络推广方式主要有
  • 怎么做品牌的官方网站百度地址如何设置门店地址
  • wordpress 收集seo推广的常见目的有
  • 大名网站建设公司美国搜索引擎排名
  • 致力于网站建设谷歌浏览器下载安卓版
  • 如何查询公司做没做网站福建搜索引擎优化
  • 网站开发语言版本不同seo综合
  • 公司做网站需要多少钱百度文章收录查询
  • 东莞公司建站哪个更便宜网络推广都是收费
  • 做产品宣传网站多少钱深圳网络推广解决方案
  • 做网站 博客家居seo整站优化方案