<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      hyy9512

      導(dǎo)航

      servlet操作本地文件匯總: 判斷文件是否存在;文件重命名;文件復(fù)制; 獲取文件屬性信息,轉(zhuǎn)成Json對象; 獲取指定類型的文件; 查找替換.txt中的文本

        1 package servlet;
        2 
        3 import java.io.BufferedReader;
        4 import java.io.File;
        5 import java.io.FileInputStream;
        6 import java.io.FileNotFoundException;
        7 import java.io.FileOutputStream;
        8 import java.io.FileReader;
        9 import java.io.FileWriter;
       10 import java.io.IOException;
       11 import java.util.ArrayList;
       12 import java.util.Date;
       13 import java.util.HashMap;
       14 import java.util.List;
       15 import java.util.Map;
       16 
       17 import javax.servlet.http.HttpServlet;
       18 
       19 import net.sf.json.JSONArray;
       20 import net.sf.json.JSONObject;
       21 
       22 public class FileActions  extends HttpServlet {
       23     private static final long serialVersionUID = 1L;
       24     /*
       25      * 判斷文件是否存在
       26      */
       27     public boolean fileIsExists(File file) {
       28         boolean bool=file.exists();
       29         return bool;
       30     }
       31     /*
       32      * 文件重命名
       33      */
       34     public boolean fileReName(String filePath, String newName) {
       35         File file=new File(filePath);
       36         boolean isExist=fileIsExists(file);
       37         boolean reName=false;
       38         if(isExist) {
       39             File newFile=new File(file.getParent()+File.separator+newName);
       40             reName=file.renameTo(newFile);
       41         } else {
       42             System.out.println("該文件不存在!");
       43         }
       44         return reName;
       45     }
       46     /*
       47      * 文件復(fù)制
       48      */
       49     public void copyFile(String filePath1,String filePath2) {
       50         File fromFile=new File(filePath1);
       51         File toFiles=new File(filePath2);
       52         boolean isExist1=fileIsExists(fromFile);
       53         if(isExist1) {
       54             try {
       55                 boolean isExist2=fileIsExists(toFiles);
       56                 if(!isExist2) 
       57                     toFiles.mkdir();
       58                 FileInputStream fis=new FileInputStream(fromFile);
       59                 File newFile=new File(toFiles.getPath()+File.separator+fromFile.getName());
       60                 FileOutputStream fos=new FileOutputStream(newFile);
       61                 int count;
       62                 byte[] buffer=new byte[1024];
       63                 while((count=fis.read(buffer))!=-1) {
       64                     for(int i=0;i<count;i++)
       65                         fos.write(buffer[i]);
       66                 }
       67             } catch (FileNotFoundException e) {
       68                 e.printStackTrace();
       69             } catch (IOException e) {
       70                 e.printStackTrace();
       71             }
       72         } else {
       73             System.out.println("該文件不存在!");
       74         }
       75     }
       76     /*
       77      * 獲取文件信息,轉(zhuǎn)成Json對象
       78      */
       79     public JSONObject getFileInfo(String filePath) {
       80         JSONObject message=new JSONObject();
       81         File file=new File(filePath);
       82         boolean isExist=fileIsExists(file);
       83         if(isExist) {
       84             if(!file.isFile()) {
       85                 message.put("message", "該路徑不是文件!");
       86             } else {
       87                 message.put("message", "文件存在!");
       88                 Map<String, String> fileInfo=new HashMap<String, String>();
       89                 try {
       90                     fileInfo.put("文件名稱", file.getName());
       91                     fileInfo.put("文件路徑", file.getCanonicalPath());
       92                     fileInfo.put("上級目錄",file.getParentFile().getParent());
       93                     fileInfo.put("隱藏",file.isHidden()?"隱藏":"顯示");
       94                     fileInfo.put("只讀屬性", file.canWrite()?"可寫":"不可寫");
       95                     fileInfo.put("最后修改日期", new Date(file.lastModified()).toLocaleString());
       96                     fileInfo.put("文件長度", String.format("%#,.2fk", file.length()/1024.0));
       97                     JSONObject jsonFileInfo=JSONObject.fromObject(fileInfo);
       98                     message.put("屬性", jsonFileInfo);
       99                 } catch (IOException e) {
      100                     e.printStackTrace();
      101                 }
      102             }
      103         } else {
      104             message.put("message", "該文件不存在!");
      105         }
      106         return message;
      107     }
      108     /*
      109      * 獲取指定類型的文件
      110      * 參數(shù)1:文件夾路徑
      111      * 參數(shù)2:指定的文件類型
      112      */
      113     public JSONArray getFileOneType(String filesPath, String type) {
      114         File[] files=null;
      115         CustomFilter fileFilter=new CustomFilter();
      116         fileFilter.setExtentName(type);
      117         File file=new File(filesPath);
      118         if(file.isDirectory()) {
      119             files=file.listFiles(fileFilter);
      120         }
      121         if(files!=null) {
      122             List<Object[]> fileList=new ArrayList<Object[]>();
      123             for(File f:files) {
      124                 Object[] subFile={f.getName(), f.length(), new Date(f.lastModified()).toLocaleString()};
      125                 fileList.add(subFile);
      126             }
      127             JSONArray jsonFile=JSONArray.fromObject(fileList);
      128             return jsonFile;
      129         } else {
      130             return null;
      131         }
      132     }
      133     /*
      134      * 查找替換.txt中的文本
      135      * 參數(shù)1: 文件路徑
      136      * 參數(shù)2: 要查找的字符串
      137      * 參數(shù)3: 替換字符串
      138      */
      139     public boolean replaceFileStr(String path, String str, String con) {
      140         try {
      141             FileReader fr=new FileReader(path);
      142             BufferedReader br=new BufferedReader(fr);
      143             char[] data=new char[1024];
      144             int rn=0;
      145             StringBuilder sb=new StringBuilder();
      146             while((rn=fr.read(data))>0) {
      147                 String content=String.valueOf(data,0,rn);
      148                 sb.append(content);
      149             }
      150             fr.close();
      151              String contentStr=sb.toString().replace(str,con);
      152              FileWriter font=new FileWriter(path);
      153              font.write(contentStr.toCharArray());
      154              font.close();
      155              return true;
      156         } catch (FileNotFoundException e) {
      157             e.printStackTrace();
      158              return false;
      159         } catch (IOException e) {
      160             e.printStackTrace();
      161              return false;
      162         }
      163     }
      164     
      165 }

       以上為servlet層代碼,下面為獲取指定類型的文件需要用到的CustomFilter類

       1 package servlet;
       2 
       3 import java.io.File;
       4 import java.io.FileFilter;
       5 
       6 public class CustomFilter implements FileFilter {
       7     private String extentName;
       8     
       9     public String getExtentName() {
      10         return extentName;
      11     }
      12 
      13     public void setExtentName(String extentName) {
      14         this.extentName = extentName;
      15     }
      16 
      17     @Override
      18     public boolean accept(File pathname) {
      19         if(extentName==null || extentName.isEmpty()) 
      20             return false;
      21         if(!extentName.startsWith("."))
      22             extentName="."+extentName;
      23         extentName=extentName.toLowerCase();
      24         if(pathname.getName().toLowerCase().endsWith(extentName))
      25             return true;
      26         return false;
      27     }
      28 
      29 }

       

      posted on 2017-12-17 14:58  hyy9512  閱讀(544)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 国产一级老熟女自拍视频| 色噜噜狠狠成人综合| 国产一区二区亚洲精品| 色AV专区无码影音先锋| 国产综合视频一区二区三区| 国产在线无码精品无码| 国产精品国产三级国产专业| 微拍福利一区二区三区| 亚洲国产精品综合久久20| 吉川爱美一区二区三区视频| 野外做受三级视频| 熟妇人妻激情偷爽文| 国产av成人精品播放| 日韩一区二区三区av在线| 午夜一区欧美二区高清三区| 精品久久人人做爽综合| 无码人妻一区二区三区线| 精品国产午夜肉伦伦影院| 会同县| 国产午夜福利不卡在线观看| 二区中文字幕在线观看| 丁香五月亚洲综合在线| 国产精品69人妻我爱绿帽子| 视频一区视频二区亚洲视频| 最近中文字幕免费手机版 | 18禁亚洲一区二区三区| 日韩av毛片福利国产福利| 亚洲va成无码人在线观看天堂| 在线无码中文字幕一区| 亚洲欧美自偷自拍视频图片| 不卡国产一区二区三区| 国产亚洲视频在线播放香蕉| 国内精品伊人久久久影视| 亚洲中文精品一区二区| 国产成人无码精品亚洲| 99www久久综合久久爱com| 精品一区二区三区四区激情| 亚洲老熟女一区二区三区| 亚洲精品中文综合第一页| 精品一区二区三区日韩版| 老熟妇乱子交视频一区|