servlet操作本地文件汇总: 判断文件是否存在;文件重命名;文件复制; 获取文件属性信息,转成Json对象; 获取指定类型的文件; 查找替换.txt中的文本
package servlet; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServlet; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class FileActions extends HttpServlet {
private static final long serialVersionUID = 1L;
/*
* 判断文件是否存在
*/
public boolean fileIsExists(File file) {
boolean bool=file.exists();
return bool;
}
/*
* 文件重命名
*/
public boolean fileReName(String filePath, String newName) {
File file=new File(filePath);
boolean isExist=fileIsExists(file);
boolean reName=false;
if(isExist) {
File newFile=new File(file.getParent()+File.separator+newName);
reName=file.renameTo(newFile);
} else {
System.out.println("该文件不存在!");
}
return reName;
}
/*
* 文件复制
*/
public void copyFile(String filePath1,String filePath2) {
File fromFile=new File(filePath1);
File toFiles=new File(filePath2);
boolean isExist1=fileIsExists(fromFile);
if(isExist1) {
try {
boolean isExist2=fileIsExists(toFiles);
if(!isExist2)
toFiles.mkdir();
FileInputStream fis=new FileInputStream(fromFile);
File newFile=new File(toFiles.getPath()+File.separator+fromFile.getName());
FileOutputStream fos=new FileOutputStream(newFile);
int count;
byte[] buffer=new byte[1024];
while((count=fis.read(buffer))!=-1) {
for(int i=0;i<count;i++)
fos.write(buffer[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("该文件不存在!");
}
}
/*
* 获取文件信息,转成Json对象
*/
public JSONObject getFileInfo(String filePath) {
JSONObject message=new JSONObject();
File file=new File(filePath);
boolean isExist=fileIsExists(file);
if(isExist) {
if(!file.isFile()) {
message.put("message", "该路径不是文件!");
} else {
message.put("message", "文件存在!");
Map<String, String> fileInfo=new HashMap<String, String>();
try {
fileInfo.put("文件名称", file.getName());
fileInfo.put("文件路径", file.getCanonicalPath());
fileInfo.put("上级目录",file.getParentFile().getParent());
fileInfo.put("隐藏",file.isHidden()?"隐藏":"显示");
fileInfo.put("只读属性", file.canWrite()?"可写":"不可写");
fileInfo.put("最后修改日期", new Date(file.lastModified()).toLocaleString());
fileInfo.put("文件长度", String.format("%#,.2fk", file.length()/1024.0));
JSONObject jsonFileInfo=JSONObject.fromObject(fileInfo);
message.put("属性", jsonFileInfo);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
message.put("message", "该文件不存在!");
}
return message;
}
/*
* 获取指定类型的文件
* 参数1:文件夹路径
* 参数2:指定的文件类型
*/
public JSONArray getFileOneType(String filesPath, String type) {
File[] files=null;
CustomFilter fileFilter=new CustomFilter();
fileFilter.setExtentName(type);
File file=new File(filesPath);
if(file.isDirectory()) {
files=file.listFiles(fileFilter);
}
if(files!=null) {
List<Object[]> fileList=new ArrayList<Object[]>();
for(File f:files) {
Object[] subFile={f.getName(), f.length(), new Date(f.lastModified()).toLocaleString()};
fileList.add(subFile);
}
JSONArray jsonFile=JSONArray.fromObject(fileList);
return jsonFile;
} else {
return null;
}
}
/*
* 查找替换.txt中的文本
* 参数1: 文件路径
* 参数2: 要查找的字符串
* 参数3: 替换字符串
*/
public boolean replaceFileStr(String path, String str, String con) {
try {
FileReader fr=new FileReader(path);
BufferedReader br=new BufferedReader(fr);
char[] data=new char[1024];
int rn=0;
StringBuilder sb=new StringBuilder();
while((rn=fr.read(data))>0) {
String content=String.valueOf(data,0,rn);
sb.append(content);
}
fr.close();
String contentStr=sb.toString().replace(str,con);
FileWriter font=new FileWriter(path);
font.write(contentStr.toCharArray());
font.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} }
以上为servlet层代码,下面为获取指定类型的文件需要用到的CustomFilter类
package servlet; import java.io.File;
import java.io.FileFilter; public class CustomFilter implements FileFilter {
private String extentName; public String getExtentName() {
return extentName;
} public void setExtentName(String extentName) {
this.extentName = extentName;
} @Override
public boolean accept(File pathname) {
if(extentName==null || extentName.isEmpty())
return false;
if(!extentName.startsWith("."))
extentName="."+extentName;
extentName=extentName.toLowerCase();
if(pathname.getName().toLowerCase().endsWith(extentName))
return true;
return false;
} }
servlet操作本地文件汇总: 判断文件是否存在;文件重命名;文件复制; 获取文件属性信息,转成Json对象; 获取指定类型的文件; 查找替换.txt中的文本的更多相关文章
- Android 关于文件及文件夹的创建 、删除、重命名、复制拷贝
package com.example.administrator.myapplication.util; import java.io.BufferedReader;import java.io.B ...
- delphi 文件的操作:重命名、复制、移动、删除
Delphi 文件的操作:重命名.复制.移动.删除第一种方法: RenameFile('Oldname', 'Newname'); CopyFile(PChar('Oldname'), PChar(' ...
- Java显示指定类型的文件
文件作为存储数据的单元,会根据数据类型产生很多分类,也就是所谓的文件类型.在对数据文件进行操作时,常常需要根据不同的文件类型来作不同的处理.本实例实现的是读取文件夹指定类型的文件并显示到表格控件中.这 ...
- linux 下文件重命名/移动/复制命令(转)
linux 下文件重命名/移动/复制命令(转) linux下重命名文件:使用mv命令就可以了, 例:要把名为:abc 重命名为:123 可以这样操作: 重命名:MV命令 1.进入你的文件目录,运行 ...
- 用Linux命令行实现删除和复制指定类型的文件
(一)Linux 删除当前目录及子目录中所有某种类型的文件 方法1 : 此方法不能处理目录中带空格的那些. rm -rf `find . -name "*.example"` Li ...
- C# 获取指定类型的文件
C# 获取指定类型的文件 public static List<FileInfo> getFile(string path, string extName) { List<FileI ...
- DevExpress的TreeList实现显示本地文件目录并自定义右键实现删除与重命名文件
场景 使用DevExpress的TreeList显示本磁盘下文件目录并在树节点上右键实现删除与添加文件. 效果 自定义右键效果 实现 首先在包含Treelist的窗体的load方法中对treelist ...
- 文件6. 查找替换.txt文本文件中的内容
servlet实现对文本文件的查找替换 .jsp界面 <form> <table> <tr> <td>选择文本文件:</td> <td ...
- Java zip 压缩 文件夹删除,移动,重命名,复制
FileUtil.java import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.ut ...
随机推荐
- 承接Unity外包 U3D外包 Unity3D外包 小型Unity项目外包用Unity还是UE4
转自mobilehub公众号(ID: mobilehub),作者:屠敏 VR浪潮席卷而来,相对于资本的狂热,现实中真正需要的是有实力的VR硬件研发团队和专业内容制作队伍.对于入门级的开发者而言,游戏引 ...
- [English] Time complexity wise this solution is the best among all
Time complexity wise this solution is the best among all, we can do all operations in O(1) time. 时间复 ...
- .class 缓存
项目用的是Ant. 场景: Class A{ private static final String HHH="hello"; } Class B{ public void met ...
- 使用 jquery.wordexport.js导出的Word排版
js导出word文档所需要的两个插件: FileSaver.js jquery.wordexport.js 使用jquery.wordexport.js这个插件导出的word文档的排版方式: 编辑器打 ...
- 记录下自己VUE项目用Hbuider打包后启动白屏问题
刚用VUE做项目,之前测试时vue创建的自身项目打包都是启动OK没问题.今天打包自己的时,启动一直白屏.折磨了好久,百度了一堆.终于找到了方法. 首先是在config/index.js里面 build ...
- 举例说明$POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LVM方式安装Ubuntu 系统
重装Ubuntu系统,使用LVM管理磁盘,最好不要使用默认的LVM安装方式,而应挂载U盘启动盘,进行物理磁盘分区,创建物理卷.卷组.虚卷(即创建LVM系列操作),在安装系统时将虚卷挂载到文件目录上.将 ...
- JDK7动态代理源码分析
IObject proxy = (IObject) Proxy.newProxyInstance(IObject.class.getClassLoader(), new Class[]{IObject ...
- vue-文字块收缩与展开功能
在设计图中要求的效果为: 文字限制超过9行即隐藏,并显示“展开”按钮,点击按钮进行切换,控制文本全部展示和部分展示 在原本的实现过程中,使用了红框内的判断方式: 页面代码: 样式则规定嵌套元素给一个死 ...
- 利用itext将html页面转成pdf(不模糊)
1.maven项目进入依赖 <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId> ...