获取项目加载根路径,得到文件夹下的所有指定后缀文件名列表

本文介绍了一个Java工具类,用于获取项目根路径、列出指定文件夹下特定后缀的文件列表及将XML文件转换为字符串。提供了详细的代码实现,并考虑了异常处理。
package com.ctfo.core.util;
import java.io.File;
importjava.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class FileAndFolderUtil {
    
    private final static Logger logger = Logger.getLogger(FileAndFolderUtil.class);
    /**
     * 获取项目加载根路径
     * @return
     */
    public static String getClassPath(){
       String strRes = "";
       try {
           strRes = FileAndFolderUtil.class.getResource("//").toURI().getPath();
       } catch (URISyntaxException e) {
           // TODO Auto-generated catch block
//         e.printStackTrace();
           logger.info("getClassPath URISyntaxException:" + e.toString());
       }
       
       return strRes;
    }
    
    /**
     * 得到文件夹下的所有指定后缀文件名列表
     * @param strFolderPath 文件夹路径
     * @param strSuffix 需要遍历的的文件后缀
     * @param blIsAbsPath 是否采用绝对路径返回
     * @return
     */
    public static List<String> getFileName(String strFolderPath, String strSuffix, boolean blIsAbsPath) {
       List<String> lsFileName = new ArrayList<String>();
       
       //用于查找文件
       File getDocument;
       if (strFolderPath == null || strFolderPath.equals("")) {
           return null;
       } else {
           if (strFolderPath.substring(strFolderPath.length()-1).equals("/")) {
               strFolderPath = strFolderPath.substring(0, strFolderPath.length()-1);
           }
           getDocument = new File(strFolderPath);
       }
       //存储文件容器
       String getFileName[];
       getFileName = getDocument.list();
       if (getFileName==null || getFileName.length<1) {
           logger.error("no file in path:" + strFolderPath + "! please check!!!");
           return null;
       }
       
       //遍历整合
       for (int i = 0; i < getFileName.length; i++) {
           //文件名合法性检查
           String strFileNameTmp = getFileName[i];
           if (strFileNameTmp.length() <= strSuffix.length()) {
               continue;
           }
           if (!strFileNameTmp.substring(strFileNameTmp.length() - strSuffix.length()).equals(strSuffix)) {
               //文件后缀不符合的情况
               continue;
           }
           //文件路径加载
           String strFileName = "";
           if (blIsAbsPath) {
               strFileName = strFolderPath + File.separator + getFileName[i];
           }else{
               strFileName = getFileName[i];
           }
           logger.debug("have loaded file =" + strFileName);
           lsFileName.add(strFileName);
       }
       return lsFileName;
    }
    
    public static String transFile2Str(String strFilePath){
       
           //创建SAXReader对象 
            SAXReader reader = new SAXReader();
            //读取文件 转换成Document 
            Document document = null;
           try {
               document = reader.read(new File(getClassPath() + strFilePath));
           } catch (DocumentException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           } 
            //document转换为String字符串 
            String documentStr = document.asXML(); 
           return documentStr;
       
    }
    
    public static List<String> getFileName(String strFolderPath, String strSuffix){
       return getFileName(strFolderPath, strSuffix, true);
    }
    
}

我想制作一个电脑可以用的单机软件,软件内容是可以在地图上做标记,在地图上标注的点可以保存图片,编辑名字,可以编辑备注信息,并且鼠标点击添加好的标注,可以显示添加的图片,我是编程小白,用的Python 和VSCode,在项目目录下创建 map.html 文件,作为地图基础模板,项目结构为:MapMarkerApp/ ├── main.py ├── map_tools.py ├── marker_dialog.py ├── map.html # 必需的主地图文件 ├── data/ (文件夹) ├── images/ (文件夹) └── icons/ (可选,存放app_icon.ico), 我想让软件每次关闭重新打开会记录上次关闭的时候地图位置大小。加个开关,点击开关后点击地图是添加标记点,关闭开关后,点击地图就不会添加标记但是可以点击标记查看标记上的内容和图片,我想再加上一点功能1,地图显示能否稍微简略一些,除了道路别的商店之类的名字不要出现。2,地图能否可以再放大一些。3,不在添加标记模式中点击标记才会显示信息和图片,显示的图片大一点,4,在添加标记模式里,点击已经添加好的标记可以编辑标记,编辑标记里面添加删除标记的按钮,便于删除标记,5.每添加一个标记后,会自动在images文件夹里面添加一个新文件夹用于存放标记里所有上传的图片,文件夹名字用地点名称命名,如果删除了标记,也相应删除对应的文件夹,在点击标记查看的小窗口里添加一个按钮,点击会自动打开标记相对应的文件夹。7,优化下编辑标点页面,上传图片按钮不要挡住已经上传的图片。
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值