正则表达式的使用 和 文件相关操作
来源:互联网 发布:驾校时间安排 知乎 编辑:程序博客网 时间:2024/05/16 17:29
输入: 文件名name,设置文件路径path
说明: 文件名为一个文档的名字,文档中有一系列 文档名和链接名;
文档中设置的文档名或者链接名规则为 [[ name]]中 name
name 如果为 a | b ,则a为文件且b为a的链接
否则 为a , a为文档
文件已经保存在path指定的路径中 or 不存在
目的: 1) 指定一个文档名str,找出该文件中所有文档文件和链接文件,还有文档str中包含的其他文档中的文档
2) 清除指定路径或文件,如果是路径,则删除整个目录
收获: 1) map<string , int > 当值为1表示文档;值为2表示链接,用于保存文件名;
这样通过一个map能够很快查找到当前文件是否已经保存
2)path.endsWith(File.separator) 判断path (string)表示的字符串是否为目录
3)temp.isDirectory()判断temp (file )是否是目录
temp.isFile() 文档
4) 获取文件内容转换为字符串
BufferedReader tBufferedReader = new BufferedReader( new FileReader (str) ); StringBuffer tStringBuffer = new StringBuffer(); String sTempOneLine = new String(""); while ((sTempOneLine = tBufferedReader.readLine()) != null){tStringBuffer.append(sTempOneLine); } tBufferedReader.close();
5) 删除文件 或目录
file.delete();
整个相关代码如下:
package src.getlinkdoc;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.HashMap;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;//setPath设置文档路径//get() 返回//Map(,1) 文档列表// Map(,2) 链接列表public class GetAllLinkAndDoc {//输入的文件名private String name;//设置文件路径private String path ="C:\\desctest";//返回数据private Map<String , Integer> data = new HashMap<String, Integer>() ;//path可为空GetAllLinkAndDoc(String str){name = str;}//关闭默认的构造函数private GetAllLinkAndDoc(){}public Boolean setStr(String str){name = str;return true;}public Boolean setPath(String path){this.path = path;return true;}//判断指定路径中str当前文件是否存在//注意: 没有考虑在子目录中查找private Boolean IsExsit(String str){File file = new File(path);String[] tempList = file.list();File temp = null;for(String s: tempList){if (path.endsWith(File.separator)) { temp = new File(path + s); } else { temp = new File(path + File.separator + s); }if (temp.isDirectory()) {//这是目录 continue; }else if(temp.isFile()){if(s.equals(str))return true;}}return false;}//判断当前文件是否已加入列表private Boolean IsInMap(String str){if( null == data ) return false;else {if(data.containsKey(str)) return true;else return false;}}//将文档转换为一个字符串// str必须是文件名,否则出错private String getStreamString(String str){ if (str != null){ try{ //如果路径名不为空就加载路径if(path != null) {if( path.endsWith(File.separator) )str+= path;else str =path + File.separator +str ;}BufferedReader tBufferedReader = new BufferedReader( new FileReader (str) ); StringBuffer tStringBuffer = new StringBuffer(); String sTempOneLine = new String(""); while ((sTempOneLine = tBufferedReader.readLine()) != null){tStringBuffer.append(sTempOneLine); } tBufferedReader.close();return new String(tStringBuffer);}catch (Exception ex){ ex.printStackTrace(); } } return null; } //从字符串中使用正则表达式获取文档或链接串private Map<String,Integer> getStrings(String str){if(str == null ) return null;Map<String,Integer> tempdata = new HashMap<String,Integer>();String regex = "\\[(img)?\\[(.*?)\\]\\]"; String str2=str; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(str2); while (m.find()){ //将文档和链接分开 String tempStr= m.group(2); if(tempStr.matches("(.*?)\\|(.*?)") ){ //是链接 String[] tempStrs = tempStr.split("\\|"); // 特例: "|"之后也可能是文档,而不是连接 //判断方法: 在当前cash中判断该文档是否存在 if(IsExsit(tempStrs[1])){ //特例部分是文档 而不是连接 tempdata.put(tempStrs[1], 1); } else { tempdata.put(tempStrs[0], 2); } } else {//是文档 tempdata.put(tempStr, 1);} } return tempdata;}//获取文件中文件名或链接private Map<String, Integer> getAll(String str){if(str.isEmpty()) return null;//将文件转换为字符串String strFile = getStreamString(str);if(strFile==null ) System.out.println("getAll.strFile is null");//字符串中获取文件和列表/*String[] strs=strFile.split("[[");*/Map<String,Integer> strMap = getStrings(strFile);/*System.out.println( strs );*/if(strMap==null ) System.out.println("getAll.strMap is null");return strMap;}//递归private void getAllText(String str){if(str == null) return ;Map<String,Integer> strMap = getAll(str);if(strMap==null ) System.out.println("getAllText.strMap is null");elsefor(Map.Entry<String,Integer> entry: strMap.entrySet()){//判断是否在文件夹中if(true == IsExsit(str) ){//不在列表中if( false == IsInMap( entry.getKey() )){data.put(entry.getKey(), entry.getValue());//是文件if(1 == entry.getValue() ){getAllText( entry.getKey() );}else {//是链表}}else{//在列表中不处理}}}}//Map(,1) 文档列表//Map(,2) 链接列表public Map<String, Integer > get(){if(name == null) return null;else {if(path == null) path = creatworkpath();if(path == null)return null;File file = new File(path);if (!file.exists()) {System.out.println("current path is not exist!");return null;}if (!file.isDirectory()) {System.out.println("current path is not a derectory!");return null;}if(name!=null){data.put(name, 1);getAllText(name);}for(Map.Entry<String, Integer> map: data.entrySet() ){System.out.println(map.getKey()+" value="+map.getValue().toString());}return data;}}public static void main(String[] args){GetAllLinkAndDoc test = new GetAllLinkAndDoc("531新一代历史库技术方案评审问题跟踪");test.get();}//删除文件及文件夹public void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容String filePath = folderPath;filePath = filePath.toString();File myFilePath = new File(filePath);myFilePath.delete(); //删除空文件夹 } catch (Exception e) { e.printStackTrace(); }} //删除指定不目录中的所有文件public boolean delAllFile(String path) {File file = new File(path);if (!file.exists()) {System.out.println("The file is not exist"); return true; }//输入的是文件就删除if (file.isFile()) {file.delete();}else if(file.isDirectory()){//是目录String[] tempList = file.list(); File temp = null;for (int i = 0; i < tempList.length; i++) {if (path.endsWith(File.separator)) {temp = new File(path + tempList[i]);} else {temp = new File(path + File.separator + tempList[i]);}if (file.isFile()) {file.delete();}if (temp.isDirectory()) {delAllFile(path + File.separator + tempList[i]);//先删除文件夹里面的文件delFolder(path + File.separator + tempList[i]);//再删除空文件夹}}}return true;}}
- 正则表达式的使用 和 文件相关操作
- 今天写了一个C#的小程序,使用了文件操作,正则表达式和XML
- js中使用正则表达式(二)操作正则表达式的对象RegExp、String和方法
- javaScript正则表达式相关函数的使用
- java正则表达式相关类的使用
- Python对unicode文件的读写和使用正则表达式
- Linux Notes:正则表达式3 文件的格式化和相关处理
- 数据库相关资料:常用的正则表达式操作符
- Python练习代码 -- 字符串和正则表达式, 文件文件夹操作
- Java 对象序列化和操作文件 正则表达式
- 正则表达式 %r{}操作符和.*? 表达式的区别
- javascript中正则表达式相关的方法使用详解
- 金额的相关正则表达式
- 正则表达式的相关知识
- 正则表达式的相关用法
- JAVA中Pattern和Matcher的用法 正则表达式相关
- 正则表达式和grep命令的使用
- 正则表达式的使用和介绍
- vs2010测试第一个OpenGL开发
- Eclipse 指定缺省的文件编码
- DOS for 用法
- mysql for linux安装步骤
- 关于CheckBox居中配置
- 正则表达式的使用 和 文件相关操作
- HDU 4681 String
- 百度地图新手教程4
- 几个html常用取尺寸属性
- APPLET签名
- GUI
- boost::spirit验证字符串是否符合四则运算规则
- 一步一步学数据结构之n--n(图--邻接链表法实现)
- dojo DataGrid使用三