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

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. Web前端总结(小伙伴的)

    以下总结是我工作室的小伙伴的心得,可以参考一下 html+css知识点总结 HTMl+CSS知识点收集 1.letter-spacing和word-spacing的区别 letter-spacing: ...

  2. [leetcode-582-Kill Process]

    Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...

  3. Example006为弹出窗口加入关闭按钮

    <!-- 实例006为弹出的窗口加入关闭按钮 --> <head> <meta charset="UTF-8"> </head> & ...

  4. android studio创建一个最简单的跳转activity

    实现目的:由mainActivity跳转到otherActivity 1.写好两个layout文件,activity_main.xml和otherxml.xml activity_main.xml & ...

  5. PowerShell使用-debug定位问题

    PowerShell就像它的名字一样,很强大,用起来很方便,所以微软基本上所有的主流企业级产品都支持PowerShell,Azure也不例外.通过Azure门户网站固然是简单直观,但对于很多IT管理员 ...

  6. Egret index.html设置

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  7. Elasticsearch学习随笔(二)-- Index 和 Doc 查询新建API总结

    本文着重总结Elasticsearch的常见API了,进行分析. Index API 初始化Index,设置shards和replica PUT http://localhost:9200/firew ...

  8. 腾讯地图JS API实现带方向箭头的线路Polyline

    最近产品提出一个需求,在我们使用的腾讯地图上为线路polyline添加线路方向.例如下图所示: 查找腾讯地图JS API提供的API,没有找到对应的支持,询问负责腾讯地图的人也得到了同样的答案,即地图 ...

  9. Bootstrap提示框

    前面的话 提示框是一个比较常见的功能,一般来说是鼠标移动到特定元素上时,显示相关的提示语.本文将详细介绍Bootstrap提示框 基本用法 Bootstrap框架中的提示框,结构非常简单,常常使用的是 ...

  10. JS - JSON.stringify