在我们的实际工作中经常会用到的文件操作,再此,将工作中碰到的做一个记录,以便日后查看。

1、复制文件夹到新文件夹下

 /**
* 复制文件夹下所有文件到指定路径
*@param oldPath
*@param newPath
*@author qin_hqing
*@date 2015年7月6日 上午11:59:33
*@comment
*/
public static 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
+ File.separator + (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.separator + file[i], newPath + File.separator + file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace(); } }

2、删除指定文件夹下的所有文件

 /**
* 删除该文件加下所有文件 - 该文件问空文件夹
*
* @param path
* @return
* @author qin_hqing
* @date 2015年6月18日 上午11:05:00
* @comment
*/
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
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 + File.separator + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + File.separator + tempList[i]);// 再删除空文件夹
flag = true;
}
}
return flag;
}
 /**
* 删除该文件夹-包含子文件及文件夹
*
* @param folderPath
* @author qin_hqing
* @return
* @date 2015年6月18日 上午11:04:13
* @comment
*/
public static boolean delFolder(String folderPath) {
boolean bl = false;
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 删除空文件夹
bl = true;
} catch (Exception e) {
e.printStackTrace();
}
return bl;
}

3、获取指定文件夹下的所有文件列表(不包含空文件夹)

 /**
* 获取指定目录下的所有文件路径
*
* @param path
* @return
* @author qin_hqing
* @date 2015年7月3日 下午5:12:59
* @comment
*/
public static List<File> getAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return list; //由于使用迭代,将list定义为全局变量
}
if (!file.isDirectory()) {
return list;
} 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()) { //如果是文件,则添加到list
list.add(temp);
}
if (temp.isDirectory()) { //如果是目录,则继续遍历
getAllFile(path + File.separator + tempList[i]);// 先获取文件夹里面的文件
}
}
return list;
}

如有遗漏,后续追加...

共勉!

Java常用文件操作-1的更多相关文章

  1. Java常用文件操作-2

    上篇文章记录了常用的文件操作,这里记录下通过SSH服务器操作Linux服务器的指定路径下的文件. 这里用到了第三方jar包 jsch-0.1.53.jar, jsch-api 1.删除服务器上指定路径 ...

  2. java常见文件操作

    收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if ( ...

  3. python 历险记(三)— python 的常用文件操作

    目录 前言 文件 什么是文件? 如何在 python 中打开文件? python 文件对象有哪些属性? 如何读文件? read() readline() 如何写文件? 如何操作文件和目录? 强大的 o ...

  4. Python之常用文件操作

    Python之常用文件操作

  5. Unix/Linux常用文件操作

    Unix/Linux常用文件操作 秘籍:man命令是Unix/Linux中最常用的命令,因为命令行命令过多,我相信每个人都会经常忘记某些命令的用法,man命令就可以显示一个命令的所有选项,参数和说明, ...

  6. 真香!Python十大常用文件操作,轻松办公

    日常对于批量处理文件的需求非常多,用Python写脚本可以非常方便地实现,但在这过程中难免会和文件打交道,第一次做会有很多文件的操作无从下手,只能找度娘. 本篇文章整理了10个Python中最常用到的 ...

  7. java中文件操作《一》

    在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.Fi ...

  8. Java 基本文件操作

    Java 文件操作 , 这也是基于Java API 操作来实现的. 文件是操作系统管理外存数据管理的基本单位, 几乎所有的操作系统都有文件管理机制. 所谓文件, 是具有符号名而且在逻辑上具有完整意义的 ...

  9. Java api 入门教程 之 JAVA的文件操作

    I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...

随机推荐

  1. php中查询mysql如何在IN array中用

    假如有一个数组 $arr = array(1,3,5,7,9)那么我在如何在php中使用mysqlWHERE id IN (1,3,5,7,9.......)$arr_string = join(', ...

  2. shell 中最常使用的 FD (file descriptor)

    在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是: 0 是一个文件描述符,表示标准输入(stdin)1 是一个文件描述符,表示标准输出(stdout) ...

  3. mysql 左连接 右连接 内链接

    一般所说的左连接,右连接是指左外连接,右外连接.做个简单的测试你看吧.先说左外连接和右外连接:[TEST1@orcl#16-12月-11] SQL>select * from t1;ID NAM ...

  4. 团队开发冲刺2-----1day

    第二冲刺阶段团队软件开发第二阶段冲刺 冲刺目标: 1.在第一阶段的基础上完成app内部界面设计. 2.逐步完成app内每一部分内容. 3.对app的实现进一步仔细钻研考虑. 4.对app每一部分内容模 ...

  5. 【LeetCode】98. Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  6. 【Android Developers Training】 76. 用Wi-Fi创建P2P连接

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 1.Smarty的下载安装

    下载地址:https://github.com/smarty-php/smarty/tree/v3.1.29 官网:smarty.net 下载解压后的目录:

  8. RedHat安装中文支持和字体

    操作系统: Red Hat Enterprise Linux 6.3 x86 安装中文语言支持: yum install "@chinese support" 安装完中文支持后,可 ...

  9. Java基础语法<一> 数据类型&运算符

    1 数据类型   1.1 整型 类型 存储需求 取值范围 int 4字节 -21 4748 3648 – 21 4748 3647 232 short 2字节 -32768-32767 216 lon ...

  10. oracle预定义角色

    角色是相关权限的集合,使用角色能够简化权限的管理.简而言之就是oracle可以事先把一系列权限集中在一起(角色),打包赋予给用户,那么用户就具有了角色的一系列权限. oracle预定义角色有25种,它 ...