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中的文本的更多相关文章

  1. Android 关于文件及文件夹的创建 、删除、重命名、复制拷贝

    package com.example.administrator.myapplication.util; import java.io.BufferedReader;import java.io.B ...

  2. delphi 文件的操作:重命名、复制、移动、删除

    Delphi 文件的操作:重命名.复制.移动.删除第一种方法: RenameFile('Oldname', 'Newname'); CopyFile(PChar('Oldname'), PChar(' ...

  3. Java显示指定类型的文件

    文件作为存储数据的单元,会根据数据类型产生很多分类,也就是所谓的文件类型.在对数据文件进行操作时,常常需要根据不同的文件类型来作不同的处理.本实例实现的是读取文件夹指定类型的文件并显示到表格控件中.这 ...

  4. linux 下文件重命名/移动/复制命令(转)

    linux 下文件重命名/移动/复制命令(转) linux下重命名文件:使用mv命令就可以了, 例:要把名为:abc   重命名为:123 可以这样操作: 重命名:MV命令 1.进入你的文件目录,运行 ...

  5. 用Linux命令行实现删除和复制指定类型的文件

    (一)Linux 删除当前目录及子目录中所有某种类型的文件 方法1 : 此方法不能处理目录中带空格的那些. rm -rf `find . -name "*.example"` Li ...

  6. C# 获取指定类型的文件

    C# 获取指定类型的文件 public static List<FileInfo> getFile(string path, string extName) { List<FileI ...

  7. DevExpress的TreeList实现显示本地文件目录并自定义右键实现删除与重命名文件

    场景 使用DevExpress的TreeList显示本磁盘下文件目录并在树节点上右键实现删除与添加文件. 效果 自定义右键效果 实现 首先在包含Treelist的窗体的load方法中对treelist ...

  8. 文件6. 查找替换.txt文本文件中的内容

    servlet实现对文本文件的查找替换 .jsp界面 <form> <table> <tr> <td>选择文本文件:</td> <td ...

  9. Java zip 压缩 文件夹删除,移动,重命名,复制

    FileUtil.java import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.ut ...

随机推荐

  1. 彻底搞懂Gradle、Gradle Wrapper与Android Plugin for Gradle的区别和联系

    首先用一段通俗易懂但是不是非常专业的话描述一下三者的概念.区别和联系. Gradle是个构建系统,能够简化你的编译.打包.测试过程.熟悉Java的同学,可以把Gradle类比成Maven. Gradl ...

  2. vue数据变动监测

    原文链接:https://blog.csdn.net/man_tutu/article/details/72148362 对象: 不能监测到: var vm = new Vue({ data:{ a: ...

  3. Flume+Sqoop+Azkaban笔记

    大纲(辅助系统) 离线辅助系统 数据接入 Flume介绍 Flume组件 Flume实战案例 任务调度 调度器基础 市面上调度工具 Oozie的使用 Oozie的流程定义详解 数据导出 sqoop基础 ...

  4. vue项目中使用插件将字符串装化为格式化的json数据(可伸缩)

    插件地址:https://www.npmjs.com/package/vue-json-viewer 第一步:安装vue-json-viewer插件 $ npm install vue-json-vi ...

  5. Altium Designer添加元件库文件

    1 默认元件库路径 C:\Users\Public\Documents\Altium\AD 10.0.0.20340\Library 2 创建元件原理图库 图2.1 新建schlib 图2.2 绘制元 ...

  6. MySQL Packets larger than max_allowed_packet are not allowed

    MySQL的一个系统参数:max_allowed_packet,其默认值为1048576(1M), 查询:show VARIABLES like '%max_allowed_packet%'; 修改此 ...

  7. nodeJs安装Vue-cli

    1,安装nodejs安装包,注意安装的路径 2,安装完成node,node有自带的npm,可以直接在cmd中,找到nodeJs安装的路径下,进行命令行全局安装vue-cli.(npm install ...

  8. RTX腾讯通字体全变成横着的了

    呵呵,简单,RTX字体选择里边的字体列表中同一种字体有些是带@符号的,有些没有带,记着选不带@号的就是头朝上的了.

  9. 【C/C++】C++11 Move, Forward

    左值与右值 Lvalue:可以出现在 operator= 左边的 Rvalue:只能出现在operator= 右边的 ; int a = b; a = b; a = a + b; a + b = a; ...

  10. div 遮罩问题

    css margin(外边距的问题): 遮罩问题:当移动一个大盒子里面的子元素的时候他的父级也会随着他的移动而移动时:就给他的父级加个透明的边框 叠加问题:当两个盒子在垂直方向移动时,给两个盒子加移动 ...