删除一个非空目录。 linux shell命令:rm -rf
点击查看代码
/**
* 删除一个非空目录。 linux shell命令:rm -rf
*
* @param path 要删除的目录。
*/
public static void deleteDirectory(String path) {
if (path == null || "".equals(path)) {
// 为空,直接返回。
return;
}
// 判断是否存在。
File file = new File(path);
if (!file.exists()) {
return;
}
// 获取目录下面的内容包括文件与目录
File[] files = file.listFiles();
// 循环迭代
for (File f : files) {
// 区分是文件,还是目录
if (f.isFile()) {
// 這個是文件,直接删除
f.delete();
} else {
// 这个是目录,通过递归来删除。
deleteDirectory(f.getAbsolutePath());
// 把自己删了。
// f.delete(); // 这个不需要,因为下面有delete()
}
}
// 上面的代码如果运行完,必须把自己也删了。
file.delete();
}
计算目录的大小。
点击查看代码
/**
* 计算目录的大小。
*
* @param path
*/
public static long computeDir(String path) {
long size = 0;
File dir = new File(path);
// 获取目录里面的内容
File[] files = dir.listFiles();
for (File file : files) { // 循环迭代。
if (file.isFile()) {
// 如果是文件直接获取大小。
size += file.length();
} else {
// 如果是目录就递归。
size += computeDir(file.getAbsolutePath());
}
}
return size;
}
实现文件的拷贝。拷贝的过程就是:从源文件里面读,然后往目标文件里面写。
点击查看代码
/**
* 实现文件的拷贝。拷贝的过程就是:从源文件里面读,然后往目标文件里面写。
* @param source 要拷贝的
* @param dest 拷贝到哪里。
*/
public static void copyFile(String source, String dest) {
InputStream is = null;
OutputStream os = null;
try {
File file = new File(source);
if (!file.exists()) {
return;
}
// 分别构建文件的输入输出流。
is = new FileInputStream(source);
os = new FileOutputStream(dest);
// 定义一个缓冲区
byte[] buffer = new byte[200];
// 每次读取的实际长度。
int len = 0;
// 先从源文件里面读取数据
while ((len = is.read(buffer)) != -1) {
// 把数据读入buffer缓冲区,然后往输出流里面写。
os.write(buffer, 0, len);// 写入缓冲区里面的数据,从缓冲区的0开始,到每次读取的数据的实际长度。
// 这里其实就是一个“边读边写”的过程,如果读完了,也就有意味着写完了,写完了,也就意味着文件拷贝完毕了。
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 无论代码有没有异常都会执行。
// 关闭
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
实现文件的拷贝(带缓冲)。拷贝的过程就是:从源文件里面读,然后往目标文件里面写。
点击查看代码
/**
* 实现文件的拷贝(带缓冲)。拷贝的过程就是:从源文件里面读,然后往目标文件里面写。
* @param source 要拷贝的
* @param dest 拷贝到哪里。
*/
public static void copyFileByBuffered(String source, String dest) {
// 构建带缓冲的输入输出流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(source));
bos = new BufferedOutputStream(new FileOutputStream(dest));
//
byte[] buffer = new byte[128];
int len = 0;
// 文件的拷贝:边读边写。
while ((len = bis.read(buffer)) != -1) {
// 写
bos.write(buffer, 0, len);
}
// 刷新缓冲
bos.flush();
} catch (Exception e) {
// 作用是:打印方法的调用信息,便于程序员查找到底是哪行代码抛异常。
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
拷贝目录:如果是文件就拷贝,如果是目录就创建。 目录下面可能还有文件与文件夹,这时候就需要递归来进行实现。
点击查看代码
/**
* 拷贝目录:如果是文件就拷贝,如果是目录就创建。 目录下面可能还有文件与文件夹,这时候就需要递归来进行实现。
*
* @param source 要拷贝的目录
* @param dest 拷贝以后的目录
* @throws Exception
*/
public static void copyDirectory(String source, String dest) throws Exception {
// 判断要拷贝的目录是否存在。
File file = new File(source);
if (!file.exists()) {
// 要拷贝的文件夹不存在
return;
}
// 拷贝后的目录要创建
File d = new File(dest);
if (!d.exists()) {
// System.out.println("创建目录" + d.getAbsolutePath());
// 创建目录
d.mkdirs();
}
// 拷贝目录:拿到目录下面的全部内容,如果是文件就拷贝,如果是目录就递归。
File[] files = file.listFiles();
if (files != null) {
// 循环迭代
for (File f : files) {
// 获取要拷贝文件/目录的绝对路径
String sourcePath = f.getAbsolutePath();
// 文件/目录的名字
String fileName = f.getName();
// 拷贝以后的文件路径:拷贝以后的目录 + 源文件的名字。
String destPath = dest + File.separator + fileName;
// 如果是文件
if (f.isFile()) {
// 拷贝文件
copyFile(sourcePath, destPath);
} else {
// 这里是目录:递归调用。
copyDirectory(sourcePath, destPath);
}
}
}
}
拷贝目录(带缓冲的):如果是文件就拷贝,如果是目录就创建。 目录下面可能还有文件与文件夹,这时候就需要递归来进行实现。
点击查看代码
/**
* 拷贝目录(带缓冲的):如果是文件就拷贝,如果是目录就创建。 目录下面可能还有文件与文件夹,这时候就需要递归来进行实现。
*
* @param source 要拷贝的目录
* @param desc 拷贝以后的目录
* @throws Exception
*/
public static void copyDirectoryByBuffered(String source, String desc) throws Exception {
// 判断要拷贝的目录是否存在。
File file = new File(source);
if (!file.exists()) {
// 要拷贝的文件夹不存在
return;
}
// 拷贝后的目录要创建
File d = new File(desc);
if (!d.exists()) {
// System.out.println("创建目录" + d.getAbsolutePath());
// 创建目录
d.mkdirs();
}
// 拷贝目录:拿到目录下面的全部内容,如果是文件就拷贝,如果是目录就递归。
File[] files = file.listFiles();
if (files != null) {
// 循环迭代
for (File f : files) {
// 获取要拷贝文件/目录的绝对路径
String sourcePath = f.getAbsolutePath();
// 文件/目录的名字
String fileName = f.getName();
// 拷贝以后的文件路径:拷贝以后的目录 + 源文件的名字。
String descPath = desc + File.separator + fileName;
// 如果是文件
if (f.isFile()) {
// 拷贝文件
copyFileByBuffered(sourcePath, descPath);
} else {
// 这里是目录:递归调用。
copyDirectoryByBuffered(sourcePath, descPath);
}
}
}
}
移动/剪切目录:先拷贝文件夹,然后把源文件夹删了。
点击查看代码
/**
* 移动/剪切目录:先拷贝文件夹,然后把源文件夹删了。
*
* @param source 要拷贝的目录
* @param dest 拷贝以后的目录
* @throws Exception
*/
public static void moveDirectory(String source, String dest) throws Exception {
// 拷贝文件夹
copyDirectory(source, dest);
// 再把源文件夹删除了
deleteDirectory(source);
}
移动/剪切目录(带缓冲):先拷贝文件夹,然后把源文件夹删了。
点击查看代码
/**
* 移动/剪切目录:先拷贝文件夹,然后把源文件夹删了。
*
* @param source 要拷贝的目录
* @param dest 拷贝以后的目录
* @throws Exception
*/
public static void moveDirectoryByBuffered(String source, String dest) throws Exception {
// 拷贝文件夹
copyDirectoryByBuffered(source, dest);
// 再把源文件夹删除了
deleteDirectory(source);
}
加密文件
点击查看代码
/**
* 加密文件。
*
* @param source 要加密的文件
* @param dest 加密以后的
* @param password 家码的密码。
*/
public static void encrypt(String source, String dest, int password) throws Exception {
// 构建文件输入流。
InputStream is = new FileInputStream(source);
// 构建文件输出流
OutputStream os = new FileOutputStream(dest);
// 每次读一个字节
int i = 0;
// 循环读
while ((i = is.read()) != -1) {
// 把读出来的字节对应的整数对密码进行“异或操作”
int value = i ^ password; //
// 再把异或异或的值写入目标文件。
os.write(value);
}
// 关闭。
os.close();
is.close();
}
解密文件:其实就是再次调用一下“加密”
点击查看代码
/**
* 解密文件:其实就是再次调用一下“加密”
*
* @param source 要解密的文件
* @param dest 解密以后的文件。
* @param password 密码。
* @throws Exception
*/
public static void decrypt(String source, String dest, int password) throws Exception {
encrypt(source, dest, password);
}
从文本文件里面按行读取,然后把每一行的数据保存到List里面。
点击查看代码
/**
* 从文本文件里面按行读取,然后把每一行的数据保存到List里面。
* @param path 要读取的文本文件的路径。
* @param encoding 按照什么编码集来读。
* @return 返回行数据。
*/
public static List<String> readFileToList(String path, String encoding) {
List<String> lines = new ArrayList<String>();
File file = new File(path);
// 对要读取的文件进行判断,是否存在。
if (!file.exists()) {
return null;// 没有就直接返回。
}
// 构建BufferedReader
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
// 按行读取
String line = null;
while ((line = br.readLine()) != null) {
// 读取了一行,保存到List里面
lines.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return lines;
}
按行写的方法的封装
点击查看代码
/**
* 按行写的方法的封装
*
* @param lines 要写的内容
* @param dest 往哪里写
* @param encoding 编码集。
*/
public static void writeFileByList(List<String> lines, String dest, String encoding) {
// 判断要写的数据
if (lines == null || lines.size() == 0) {
// 要写的数据为空,直接返回。
return;
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(dest)), encoding));
// 按行写:迭代List然后写。
for (String line : lines) {
// 写
bw.write(line);
// 换行
bw.newLine();
}
// 关闭
bw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
整理目录
点击查看代码
/**
* 整理目录
*
* @param source
* @param dest
*/
@SuppressWarnings("unused")
public static void tidyDirtectory(String source, String dest) {
// 判断要拷贝的目录是否存在。
File file = new File(source);
if (!file.exists()) {
// 要拷贝的文件夹不存在
return;
}
// 拷贝目录:拿到目录下面的全部内容,如果是文件就拷贝,如果是目录就递归。
File[] files = file.listFiles();
if (files != null) {
// 循环迭代
for (File f : files) {
// 获取要拷贝文件/目录的绝对路径
String sourcePath = f.getAbsolutePath();
// 文件/目录的名字
String fileName = f.getName();
// 如果是文件
if (f.isFile()) {
// 获取文件的后缀:xxx.java获取java。
String fileType = fileName;
if (fileName.indexOf(".") > 0) {// 判断文件名有没有后缀。
fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
}
// 归类后的目录要创建:按照类型
File d = new File(dest + File.separator + fileType);
if (!d.exists()) {
// 创建目录
d.mkdirs();
}
// 拷贝以后的文件路径:拷贝以后的目录 + 文件类型 + 源文件的名字。
String descPath = dest + File.separator + fileType + File.separator + fileName;
String fileTime = new DateUtils().formatDate(new Date(f.lastModified()), "yyyy-MM-dd HH:mm:ss:SSS");
// System.out.println(fileName + ", " + fileTime);
// 拷贝文件
copyFileByBuffered(sourcePath, descPath);
} else {
// 这里是目录:递归调用。
tidyDirtectory(sourcePath, dest);
}
}
}
}
整理图片,按照图片的创建日期整理。
点击查看代码
/**
* 整理图片,按照图片的创建日期整理。
*
* @param source 要整理照片的目录
* @param dest 整理后的图片放哪里。
* @param timePattern 存放的文件夹日期格式
*/
public static void tidyPhotoByDirtectory(String source, String dest, String timePattern) {
// 判断要拷贝的目录是否存在。
File file = new File(source);
if (!file.exists()) {
// 要拷贝的文件夹不存在
return;
}
// 拷贝目录:拿到目录下面的全部内容,如果是文件就拷贝,如果是目录就递归。
File[] files = file.listFiles();
if (files != null) {
// 循环迭代
for (File f : files) {
// 获取要拷贝文件/目录的绝对路径
String sourcePath = f.getAbsolutePath();
// 文件/目录的名字
String fileName = f.getName();
// 如果是文件
if (f.isFile()) {
// 文件时间。
String fileTime = new DateUtils().formatDate(new Date(f.lastModified()), timePattern);
// 归类后的目录要创建:按照类型
File d = new File(dest + File.separator + fileTime);
if (!d.exists()) {
// 创建目录
d.mkdirs();
}
// 拷贝以后的文件路径:拷贝以后的目录 + 文件类型 + 源文件的名字。
String destPath = dest + File.separator + fileTime + File.separator + fileName;
// System.out.println(fileName + ", " + fileTime);
// 拷贝文件
copyFileByBuffered(sourcePath, destPath);
} else {
// 这里是目录:递归调用。
tidyDirtectory(sourcePath, dest);
}
}
}
}