import java.io.*;

public class FileOperate {

public FileOperate() {

}

/**

* 新建目录

* @param folderPath String 如 c:/fqf

* @return boolean

*/

public void newFolder(String folderPath) {

try {

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath = new java.io.File(filePath);

if (!myFilePath.exists()) {

myFilePath.mkdir();

}

}

catch (Exception e) {

System.out.println( "新建目录操作出错 ");

e.printStackTrace();

}

}

/**

* 新建文件

* @param filePathAndName String 文件路径及名称 如c:/fqf.txt

* @param fileContent String 文件内容

* @return boolean

*/

public void newFile(String filePathAndName, String fileContent) {

try {

String filePath = filePathAndName;

filePath = filePath.toString();

File myFilePath = new File(filePath);

if (!myFilePath.exists()) {

myFilePath.createNewFile();

}

FileWriter resultFile = new FileWriter(myFilePath);

PrintWriter myFile = new PrintWriter(resultFile);

String strContent = fileContent;

myFile.println(strContent);

resultFile.close();

}

catch (Exception e) {

System.out.println( "新建目录操作出错 ");

e.printStackTrace();

}

}

/**

* 删除文件

* @param filePathAndName String 文件路径及名称 如c:/fqf.txt

* @param fileContent String

* @return boolean

*/

public void delFile(String filePathAndName) {

try {

String filePath = filePathAndName;

filePath = filePath.toString();

java.io.File myDelFile = new java.io.File(filePath);

myDelFile.delete();

}

catch (Exception e) {

System.out.println( "删除文件操作出错 ");

e.printStackTrace();

}

}

/**

* 删除文件夹

* @param filePathAndName String 文件夹路径及名称 如c:/fqf

* @param fileContent String

* @return boolean

*/

public void delFolder(String folderPath) {

try {

delAllFile(folderPath); //删除完里面所有内容

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath = new java.io.File(filePath);

myFilePath.delete(); //删除空文件夹

}

catch (Exception e) {

System.out.println( "删除文件夹操作出错 ");

e.printStackTrace();

}

}

/**

* 删除文件夹里面的所有文件

* @param path String 文件夹路径 如 c:/fqf

*/

public void delAllFile(String path) {

File file = new File(path);

if (!file.exists()) {

return;

}

if (!file.isDirectory()) {

return;

}

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 (temp.isFile()) {

temp.delete();

}

if (temp.isDirectory()) {

delAllFile(path+ "/ "+ tempList[i]);//先删除文件夹里面的文件

delFolder(path+ "/ "+ tempList[i]);//再删除空文件夹

}

}

}

/**

* 复制单个文件

* @param oldPath String 原文件路径 如:c:/fqf.txt

* @param newPath String 复制后路径 如:f:/fqf.txt

* @return boolean

*/

public void copyFile(String oldPath, String newPath) {

try {

int bytesum = 0;

int byteread = 0;

File oldfile = new File(oldPath);

if (oldfile.exists()) { //文件存在时

InputStream inStream = new FileInputStream(oldPath); //读入原文件

FileOutputStream fs = new FileOutputStream(newPath);

byte[] buffer = new byte[1444];

int length;

while ( (byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; //字节数 文件大小

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

}

}

catch (Exception e) {

System.out.println( "复制单个文件操作出错 ");

e.printStackTrace();

}

}

/**

* 复制整个文件夹内容

* @param oldPath String 原文件路径 如:c:/fqf

* @param newPath String 复制后路径 如:f:/fqf/ff

* @return boolean

*/

public void copyFolder(String oldPath, String newPath) {

try {

(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹

File a=new File(oldPath);

String[] file=a.list();

File temp=null;

for (int i = 0; i < file.length; i++) {

if(oldPath.endsWith(File.separator)){

temp=new File(oldPath+file[i]);

}

else{

temp=new File(oldPath+File.separator+file[i]);

}

if(temp.isFile()){

FileInputStream input = new FileInputStream(temp);

FileOutputStream output = new FileOutputStream(newPath + "/ " +

(temp.getName()).toString());

byte[] b = new byte[1024 * 5];

int len;

while ( (len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

output.close();

input.close();

}

if(temp.isDirectory()){//如果是子文件夹

copyFolder(oldPath+ "/ "+file[i],newPath+ "/ "+file[i]);

}

}

}

catch (Exception e) {

System.out.println( "复制整个文件夹内容操作出错 ");

e.printStackTrace();

}

}

/**

* 移动文件到指定目录

* @param oldPath String 如:c:/fqf.txt

* @param newPath String 如:d:/fqf.txt

*/

public void moveFile(String oldPath, String newPath) {

copyFile(oldPath, newPath);

delFile(oldPath);

}

/**

* 移动文件到指定目录

* @param oldPath String 如:c:/fqf.txt

* @param newPath String 如:d:/fqf.txt

*/

public void moveFolder(String oldPath, String newPath) {

copyFolder(oldPath, newPath);

delFolder(oldPath);

}

}

java文件删除操作_Java文件复制删除操作合集相关推荐

  1. stata代码命令全集:跟检验do文件、stata常用命令、代码大合集、面板熵值法

     一.常用模型代码整理 1.数据来源: 2.时间跨度:无 3.区域范围:无 4.指标说明: 包含如下模型代码: l OLS模型 l Heckman两阶段模型 l PSM+DID模型 l 固定效应模型( ...

  2. 解决打开文件、文件夹、拖拽复制删除时鼠标卡顿

    1.问题描述 打开文件.文件夹.拖拽文件复制删除时,鼠标总会卡一两秒,在需要处理大量文件时非常影响工作. 2.解决过程 通过任务管理器发现windows资源管理器在进行上述操作时cpu占用会迅速拉高, ...

  3. java多级目录文件是否存在_Java文件夹操作,判断多级路径是否存在,不存在就创建(包括windows和linux下的路径字符分析)...

    兼容windows和linux. 分析: 在windows下路径有以下表示方式: (标准)D:\test\1.txt (不标准,参考linux)D:/test/1.txt 然后在java中,尤其使用F ...

  4. java文件tree目录_java 遍历目录,操作文件 tree命令

    用tree命令显示目录.文件结构 有时候我们想了解一个文件夹或者驱动器根目录下的所有文件,并希望它以资源管理器的树形视图方式显示文件结构.可以在"命令提示符"窗口中输入" ...

  5. java文件操作_Java文件操作大全

    文件属性的取得 文件属性的取得 String path=request.getRealPath("/"); File f=new File(path,"ReadData. ...

  6. java 处理换行符_Java 文件换行符识别与转换

    项目经验,如需转载,请注明作者:Yuloran (t.cn/EGU6c76) 背景 项目开发需要手动合入几十种语言的翻译到 string.xml 中,这是一件非常痛苦的事情:Copy.Paste,Co ...

  7. java文件与流_Java文件与流

    文件 文件管理通过java.io包下.file类 作用: 1.文件属性 2.文件检查 3.文件删除 4.不包含对其文件内容的处理 File类的构造 语法: File f =new File(Strin ...

  8. java文件上传_Java文件上传细讲

    什么是文件上传? 文件上传就是把用户的信息保存起来. 为什么需要文件上传? 在用户注册的时候,可能需要用户提交照片.那么这张照片就应该要进行保存. 上传组件(工具) 为什么我们要使用上传工具? 为啥我 ...

  9. java 分块上传_Java 文件分块上传客户端和服务器端源代码

    本博客介绍如何进行文件的分块上传.本文侧重介绍客户端,服务器端请参考博客<Java 文件分块上传服务器端源代码>.建议读者朋友在阅读本文代码前先了解一下 MIME 协议. 所谓分块上传并非 ...

  10. java 文件写入 读取_JAVA文件的两种读取方法和三种写入方法

    在使用java对文件进行读写操作时,有多种方法可以使用,但不同的方法有不同的性能. 此文对常用的读写方法进行了整理,以备不时之需. 1.文件的读取 主要介绍两种常用的读取方法.按行读取和按字符块读取. ...

最新文章

  1. php与c 哪个好,C语言和PHP,新手选择哪个比较好?
  2. 基因测序3——三、四代测序技术来势汹汹,国产化仍在布局二代测序?
  3. oracle SQL 命令行(二.视图)
  4. 代码: html 页面小效果 (集合,待补充)
  5. Elasticsearch-06 Spring Boot 2.0.9整合ElasticSearch5.6.16
  6. Py之Xlrd:Xlrd的使用方法总结(获取的sheet名字/sheet索引/sheet内容/数和列数、获取整行和整列的值(列表) 、指定单元格的内容/数据类型)之详细攻略
  7. CentOS学习笔记--目录配置
  8. leetcode 402. Remove K Digits | 402. 移掉 K 位数字(单调栈)
  9. java interfaceof,java interface教程
  10. python-flask-Flask-SQLAlchemy与Flask-Migrate联合进行数据化迁移
  11. 数字表达_英语数字表达方法大全,内含数字/分数/时间/序数词/日期/小数等
  12. c++MFC 截取字符串
  13. 【高频电子线路】[模型]阻抗变换与阻抗匹配(第2章 谐振功率放大器)
  14. 春节要闻回顾 | 数字人民币和元宇宙被搬上春晚;美国财政部报告警告NFT可用于洗钱...
  15. 二分类模型评价指标-AUC
  16. 管理的5个层次,你在第几层?
  17. 面试官问:“在项目中用过多线程吗?”你就把这个案例讲给他听!
  18. Mac系统瘦身存储清理
  19. 《我的团长我的团》书及电视剧观后感
  20. 鸽巢排序Pigeonhole sort

热门文章

  1. ngx_http_upstream_module模块学习笔记
  2. DP:最大公共子序列
  3. 并发编程学习之ForkJoinPool分支合并
  4. DelayedWorkQueue踩坑笔记
  5. 了解一下Redis队列【缓兵之计-延时队列】
  6. 1.Java集合-HashMap实现原理及源码分析
  7. zepto,kissy前端框架实现跨域
  8. Matlab DIP(瓦)ch5图像复原练习
  9. Windows Phone实用开发技巧(33):不重启程序切换当前语言
  10. 通过ssh登陆linux后使用vim时按了ctrl+s