Java短视频小程序开发 全栈式实战项目【2021最新升级版】
download:Java短视频小程序开发 全栈式实战项目【2021最新升级版】 *
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
- @author s.watson
*/
public FileTools() {
}
/**
- formatPath 转义文件目录
* - @param path
- @return
*/
return path.replaceAll("\\\\", "/");
}
/**
- combainPath文件路径合并
* - @param eins
- @param zwei
- @return
*/
String dori = "";
eins = null == eins ? "" : formatPath(eins);
zwei = null == zwei ? "" : formatPath(zwei);
if (!eins.endsWith("/") && zwei.indexOf("/") != 0) {
dori = eins + "/" + zwei;
} else {
dori = (eins + zwei).replaceAll("//", "/");
}
return dori;
}
/**
- list2Array 列表转换数组
* - @param list
- @return
*/
String array[] = (String[]) list.toArray(new String[list.size()]);
return array;
}
/**
- cp 复制文件
* - @param source
- @param destination
- @param loop
- @return
*/
List list = new ArrayList();
try {
File srcFile = new File(source);
File desFile = new File(destination);
list.addAll(cp(srcFile, desFile, loop));
} catch (Exception ex) {
j2log(null, null, ex);
}
return list;
}
/**
- cp 复制文件
* - @param source
- @param destination
- @param loop
- @return
*/
List list = new ArrayList();
try {
if (!source.exists() || source.isDirectory()) {
throw new FileNotFoundException();
}
list.add(cp(source, destination));
if (loop) {
String[] subFile = source.list();
for (String subPath : subFile) {
String src = https://www.it610.com/article/combainPath(source.getPath(), subPath);
//子文件原文件路径
String des = combainPath(destination.getPath(), subPath);
//子文件目标路径
File subfile = new File(src);
if (subfile.isFile()) {
list.add(cp(src, des));
} else if (subfile.isDirectory() && loop) {
list.addAll(cp(src, des, loop));
}
}
}
} catch (Exception ex) {
j2log(null, null, ex);
}
return list;
}
/**
- cp 单文件复制文件
* - @param source
- @param destination
- @return
*/
File desFile = null;
try {
File srcFile = new File(source);
desFile = new File(destination);
desFile = cp(srcFile, desFile);
} catch (Exception ex) {
j2log(null, null, ex);
}
return desFile;
}
/**
- cp 单文件复制文件
* - @param source
- @param destination
- @return
*/
FileInputStream in = null;
FileOutputStream out = null;
FileChannel inc = null;
FileChannel outc = null;
try {
if (!source.exists() || source.isDirectory()) {
throw new FileNotFoundException();
}
if (source.getPath().equals(destination.getPath())) {
return source;
}
long allbytes = du(source, false);
if (!destination.exists()) {
destination.createNewFile();
}
in = new FileInputStream(source.getPath());
out = new FileOutputStream(destination);
inc = in.getChannel();
outc = out.getChannel();
ByteBuffer byteBuffer = null;
long length = 2097152;
//基本大小,默认2M
long _2M = 2097152;
while (inc.position() < inc.size()) {
if (allbytes > (64 * length)) {//如果文件大小大于128M 缓存改为64M
length = 32 * _2M;
} else if (allbytes > (32 * length)) {//如果文件大小大于64M 缓存改为32M
length = 16 * _2M;
} else if (allbytes > (16 * length)) {//如果文件大小大于32M 缓存改为16M
length = 8 * _2M;
} else if (allbytes > (8 * length)) {//如果文件大小大于16M 缓存改为8M
length = 4 * _2M;
} else if (allbytes > (4 * length)) {//如果文件大小大于8M 缓存改为4M
length = 2 * _2M;
} else if (allbytes > (2 * length)) {//如果文件大小大于4M 缓存改为2M
length = _2M;
} else if (allbytes > (length)) {//如果文件大小大于2M 缓存改为1M
length = _2M / 2;
} else if (allbytes < length) {//如果文件小于基本大小,直接输出
length = allbytes;
}
allbytes = inc.size() - inc.position();
byteBuffer = ByteBuffer.allocateDirect((int) length);
inc.read(byteBuffer);
byteBuffer.flip();
outc.write(byteBuffer);
outc.force(false);
}
} catch (Exception ex) {
j2log(null, null, ex);
} finally {
try {
if (null != inc) {
inc.close();
}
if (null != outc) {
outc.close();
}
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
} catch (Exception ex) {
j2log(null, null, ex);
}
}
return destination;
}
/**
- rename 文件重命名
* - @param filePath
- @param from
- @param to
- @return
*/
File newFile = null;
try {
File oldFile = new File(combainPath(filePath, from));
newFile = new File(combainPath(filePath, to));
rename(newFile, oldFile);
} catch (Exception ex) {
j2log(null, null, ex);
}
return newFile;
}
/**
- rename 文件重命名
* - @param to
- @param from
- @return
*/
try {
String newPath = to.getPath();
String oldPath = from.getPath();
if (!oldPath.equals(newPath)) {
if (!to.exists()) {
from.renameTo(to);
}
}
} catch (Exception ex) {
j2log(null, null, ex);
}
return to;
}
/**
- mv 移动文件
* - @param fileName
- @param source
- @param destination
- @param cover
*/
try {
if (!source.equals(destination)) {
File oldFile = new File(combainPath(source, fileName));
File newFile = new File(combainPath(destination, fileName));
mv(oldFile, newFile, cover);
}
} catch (Exception ex) {
j2log(null, null, ex);
}
}
/**
- mv 移动文件
* - @param source
- @param destination
- @param cover
*/
try {
if (!source.equals(destination)) {
File oldFile = new File(source);
File newFile = new File(destination);
mv(oldFile, newFile, cover);
}
} catch (Exception ex) {
j2log(null, null, ex);
}
}
/**
- mv 移动文件
* - @param source
- @param destination
- @param cover
*/
try {
if (!source.exists()) {
throw new FileNotFoundException();
}
StringBuilder fileName = new StringBuilder(source.getName());
if (!cover && source.getPath().equals(destination.getPath())) {
if (fileName.indexOf(".") > 0) {
fileName.insert(fileName.lastIndexOf("."), "_副本");
} else {
fileName.append("_副本");
}
cp(source, new File(combainPath(source.getParent(), fileName.toString())));
} else {
source.renameTo(destination);
}
} catch (Exception ex) {
j2log(null, null, ex);
}
}
/**
- extensions 获取文件扩展名信息
* - @param filePath
- @param fileName
- @return
*/
String[] extension = {};
try {
String fullPath = combainPath(filePath, fileName);
File file = new File(fullPath);
extensions(file);
} catch (Exception ex) {
j2log(null, null, ex);
}
return extension;
}
/**
- extensions 获取文件扩展名信息
* - @param fullPath
- @return
*/
String[] extension = {};
try {
File file = new File(fullPath);
extensions(file);
} catch (Exception ex) {
j2log(null, null, ex);
}
return extension;
}
/**
- extensions 获取文件扩展名信息
* - @param file
- @return
*/
String[] extension = {};
try {
if (file.isFile()) {
String fileName = file.getName();
if (fileName.lastIndexOf(".") >= 0) {
int lastIndex = fileName.lastIndexOf(".");
extension[0] = String.valueOf(lastIndex);
//扩展名的“.”的索引
extension[1] = fileName.substring(lastIndex + 1);
//扩展名
extension[2] = fileName.substring(0, lastIndex);
//文件名
}
}
} catch (Exception ex) {
j2log(null, null, ex);
}
return extension;
}
/**
- ls 遍历文件
* - @param filePath
- @param loop
- @return
*/
List list = new ArrayList();
try {
File file = new File(filePath);
list.addAll(ls(file, loop));
} catch (Exception ex) {
j2log(null, null, ex);
}
return list;
}
/**
- ls 遍历文件
* - @param file
- @param loop
- @return
*/
List list = new ArrayList();
try {
list.add(file);
if (!file.isDirectory()) {
list.add(file);
} else if (file.isDirectory()) {
File[] subList = file.listFiles();
subList = filesSort(subList, true);
for (File subFile : subList) {
if (subFile.isDirectory() && loop) {
list.addAll(ls(subFile.getPath(), loop));
} else {
list.add(subFile);
}
}
}
} catch (Exception ex) {
j2log(null, null, ex);
}
return list;
}
/**
- filesSort 文件排序(默认升序)
* - @param parentPath
- @param subList
- @return
*/
List files = new ArrayList();
List dirs = new ArrayList();
for (File subFile : inFiles) {
if (subFile.isDirectory()) {
dirs.add(subFile.getPath());
} else if (subFile.isFile()) {
files.add(subFile.getPath());
}
}
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 两短篇
- 4月23日海军节,我在青岛等你,一起看强大的中国海军。(如图如视频)
- 视频转换器哪种好用()
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- 不懂法,害人终害己