JAVA操作文件大全(二)
/**
* 删除文件夹里面的所有文件
* @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操作文件大全(一)
package com.pengyue; import java.io.*; public class FileOperate { public FileOperate() { ...
- java读写文件大全
使用Java操作文本文件的方法详解 [http://blog.csdn.net/smartcat86/article/details/4085739/] 摘要: 最初java是不支持对文本文件的处理的 ...
- Java操作数据库方式二DBCP使用详解
##概述 DBCP的全称是:DataBase connection pool,翻译是:数据库连接池. 在Java操作数据库方式一JDBC使用详解中说到直接使用JDBC非常消耗资源.为了避免频繁关闭链接 ...
- java操作文件爱女_Java的IO操作---File类
目标 1)掌握File类作用 2)可以使用file类中方法对文件进行读写操作. File类 唯一与文件有关的类.使用file类可进行创建或删除操作,要想使用File类,首先观察File类的构造方法. ...
- java复制重命名,JAVA操作文件的复制和重命名失败的原因
最近在coding Java复制文件夹的时候,比较懒,直接百度了一个方法,直接使用到项目中,后面再调用重命名方法的时候,总是返回false,"视野"不开阔,百度了很久不思其解.&q ...
- ASP.NET操作文件大全
1.重命名文件名称 string 新文件名 = Guid.NewGuid().ToString("N"); /// <summary> /// 创建年月日文件夹 /// ...
- Java操作文件Path 和 Paths 及Files类介绍
我们平时操作文件时可能要写一大堆代码.JDK1.7引入了好用的Path 和 Paths类. 创建文件,删除文件,复制文件,查找文件,查找文件个数等,只需要调一个Api就能解决,下面开始进行介绍. 1. ...
- java操作_JAVA操作文件大全(一)
package com.pengyue; import java.io.*; public class FileOperate { public FileOperate() { } /** * 新建目 ...
- Java操作文件的方法大全
标题 最新工作涉及到文件操作的内容,顺带花点时间把各种业务场景的方法都总结出来,供大家参考 对应Maven依赖.这块提醒千万别倒错包,这块的方法本人都亲测过,如果报错看下依赖是否正确 <depe ...
最新文章
- 数据库1.0 -- 数据库的基本操作
- python源代码不需要编译成什么-编译 Python 源代码
- 汇编:内存地址为什么从0开始?等问题
- 凸透镜成像动画可拖动_光学实验二:探究凸透镜成像规律
- angularjs 事件指令
- 富文本框让最大四百像素_富文本框的使用
- oracle sql between and 范围,oracle between and 边界有关问题
- Vert.x 异步访问数据库 MySQL
- 5.5 tensorflow2实现多项式回归与神经网络、未来一个月购买量预测——python实战
- python 移动文件位置-python工具-文件及文件夹移动及处理
- MySQL主从配置(Django实现主从配置读写分离)
- 推荐两个在线代理服务器
- Android拓展系列(5)--CyanogenMod源码下载和编译(Android ROM定制基础篇)
- Quartus ii 中ROM ip核的应用
- 项目管理之项目整体管理
- 22KDD : COSTA Covariance-Preserving Feature Augmentation for Graph Contrastive Learning
- 成都榆熙教育咨询有限公司:拼多多电商新经济,个人电商如何参与?
- 使用ffmpeg读取本地文件,进行推流
- 基于热销商品宝贝的数据调用分析
- std::string 含义