org.easyarch.netpet.kits.file.FileKits Maven / Gradle / Ivy
The newest version!
package org.easyarch.netpet.kits.file;/**
* Description :
* Created by YangZH on 16-10-27
* 下午7:42
*/
import org.easyarch.netpet.kits.HashKits;
import java.io.*;
import java.io.FileFilter;
import java.util.*;
/**
* Description :
* Created by YangZH on 16-10-27
* 下午7:42
*/
public class FileKits {
public static final long ONE_KB = 1024;
/**
* The number of bytes in a megabyte.
*/
public static final long ONE_MB = ONE_KB * ONE_KB;
/**
* The number of bytes in a gigabyte.
*/
public static final long ONE_GB = ONE_KB * ONE_MB;
public static boolean isDir(String path) {
if (path == null)
return false;
return isDir(new File(path));
}
public static boolean isDir(File file) {
if (file == null)
return false;
return file.isDirectory();
}
public static boolean exists(String path) {
if (path == null)
return false;
return exists(new File(path));
}
public static boolean exists(File file) {
return file == null ? false : file.exists();
}
private static File create(String path, boolean isFile) throws Exception {
if (path == null)
return null;
return create(new File(path), isFile);
}
private static File create(File file, boolean isFile) throws Exception {
if (file == null)
return null;
if (!file.exists()) {
if (isFile) {
file.createNewFile();
} else {
file.mkdirs();
}
}
return file;
}
public static List ls(String path) throws Exception {
if (path == null)
return null;
return ls(new File(path));
}
public static List ls(File file) throws Exception {
if (!exists(file)) {
throw new FileNotFoundException("文件" + file.getAbsolutePath() + "不存在");
}
return Arrays.asList(file.listFiles());
}
public static File touch(String path) throws Exception {
return create(path, true);
}
public static File touch(File file) throws Exception {
return create(file, true);
}
public static File mkdir(String dir) throws Exception {
return create(dir, false);
}
public static File mkdir(File dir) throws Exception {
return create(dir, false);
}
public static String cat(String path) throws Exception {
if (path == null)
throw new NullPointerException("path is null");
return cat(new File(path));
}
public static String cat(File file) throws Exception {
if (file == null)
throw new NullPointerException("file is null");
if (!file.exists()) {
throw new FileNotFoundException("文件" + file.getAbsolutePath() + "不存在");
}
InputStream is = new FileInputStream(file);
String result = IOKits.toString(is);
IOKits.closeIO(is);
return result;
}
public static void cp(String srcPath, String dstPath) throws Exception {
if (srcPath == null)
throw new NullPointerException("path is null");
cp(new File(srcPath), new File(dstPath));
}
public static void cp(File src, File dst) throws Exception {
if (src == null)
throw new NullPointerException("file is null");
if (!exists(src)) {
throw new FileNotFoundException("文件" + src.getAbsolutePath() + "不存在");
}
if (!exists(dst)) {
dst.createNewFile();
}
write(dst,read(src));
}
public static File vim(String path, String str) throws Exception {
return write(path, str.getBytes());
}
public static File write(String path, byte[] data) throws Exception {
if (path == null)
throw new NullPointerException("path is null");
return write(new File(path), data);
}
public static File write(File file, byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
FileOutputStream fos = new FileOutputStream(file);
IOKits.transferFrom(bais, fos);
bais.close();
fos.close();
return file;
}
public static byte[] read(String path) throws Exception {
if (path == null)
return null;
return read(new File(path));
}
public static byte[] read(File path) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = null;
fis = new FileInputStream(path);
IOKits.transferTo(fis, baos);
IOKits.closeIO(baos);
IOKits.closeIO(fis);
return baos.toByteArray();
}
public static byte[] readx(String path){
File file = new File(path);
try {
RandomAccessFile raf = new RandomAccessFile(file,"r");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOKits.bigCopy(raf.getChannel(),baos);
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据分隔符统计文本文件被分成多少块
* @param file 文件
* @param separator 分隔符
* @return 返回被切分的块数
*/
public static int statistic(File file,String separator) throws Exception {
String content = cat(file);
String[] segements = content.split(separator);
return segements.length;
}
/**
* 根据分隔符统计文本文件被分成多少块
* @param path 文件路径
* @param separator 分隔符
* @return 返回被切分的块数
*/
public static int statistic(String path,String separator) throws Exception {
return statistic(new File(path),separator);
}
public static String getName(String filePath){
String[] seg = filePath.split(File.separator);
return seg[seg.length - 1];
}
public static File vim(File file, String str) throws Exception {
return write(file, str.getBytes());
}
/**
* 清空指定根目录下所有的空文件夹
* @param directory
* @return
*/
public static int deletEmptyDirectory(File directory) {
int count = 0;
if (directory == null || directory.list() == null
|| directory.isFile() || directory.list().length == 0){
return 0;
}else {
File[] files = directory.listFiles();
for (File f : files) {
if (f.isDirectory()){
if (!f.delete()){//删除该文件夹,删除失败则进入目录删除
count += deletEmptyDirectory(f);
if (f.delete()){
count++;
}
}else{
count++;
}
}
}
}
return count;
}
public static int deletEmptyDirectory(String path){
return deletEmptyDirectory(new File(path));
}
public static String getBottomDir(String filePath){
return getBottomDir(new File(filePath)).getPath() + File.separator;
}
/**
* 根据当前文件获得其最底层目录
* 先把当前文件名去掉,得到的就是最底层目录了
* @param file
* @return
*/
public static File getBottomDir(File file){
String filePath = file.getPath();
int lastSeparatorIndex = filePath.lastIndexOf("/") + 1;
String configFileName = filePath.substring(
lastSeparatorIndex,filePath.length());
int fileNameIndex = filePath.lastIndexOf(configFileName);
String bottomDir = filePath.substring(0,fileNameIndex);
return new File(bottomDir);
}
/**
* 递归遍历
* @param file
* @param includeDir
* @return
*/
private static List listRecursive(File file,boolean includeDir){
if (file == null){
return null;
}
List paths = new ArrayList();
File[] files = file.listFiles();
for (File f:files){
if (f.isDirectory()){
if (includeDir)
paths.add(f);
paths.addAll(listRecursive(f,includeDir));
}else{
paths.add(f);
}
}
return paths;
}
public static List listFileRecursive(File file){
return listRecursive(file,false);
}
public static List listFileRecursive(String path){
return listFileRecursive(new File(path));
}
public static List listDirectoryRecursive(File file){
return listRecursive(file,true);
}
public static List listDirectoryRecursive(String path){
return listDirectoryRecursive(new File(path));
}
public static Date getLastModifyTime(String path){
File file = new File(path);
return new Date(file.lastModified());
}
public static boolean isFileNewer(File file, long timeMillis) {
if (file == null) {
throw new NullPointerException("file is null");
}
if (exists(file)) {
return false;
}
return file.lastModified() > timeMillis;
}
public static boolean isFileNewer(File file, Date date) {
if (file == null) {
throw new NullPointerException("file is null");
}
if (exists(file)) {
return false;
}
return file.lastModified() > date.getTime();
}
public static boolean eq(File file1, File file2) {
if (!exists(file1) || !exists(file2)) {
return false;
}
if (file1 == file2) {
return true;
}
if (file1.isDirectory() || file2.isDirectory()) {
throw new IllegalArgumentException("Can't compare directories");
}
try {
return IOKits.equals(new FileInputStream(file1), new FileInputStream(file2));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static boolean eq(String path1, String path2) {
return eq(new File(path1), new File(path2));
}
public static List filter(File file,FileFilter filter){
if (file == null){
return null;
}
List paths = new ArrayList();
File[] files = file.listFiles();
for (File f:files){
if (f.isDirectory()){
paths.addAll(filter(f,filter));
}else if (filter.accept(f)){
paths.add(f);
}
}
return paths;
}
public static List filter(String path,FileFilter filter){
return filter(new File(path),filter);
}
public static String md5(String path) throws Exception {
return HashKits.md5(read(path));
}
public static String md5(File file) throws Exception {
return md5(file.getPath());
}
public static String byteCountToDisplaySize(long size) {
String displaySize;
if (size / ONE_GB > 0) {
displaySize = String.valueOf(size / ONE_GB) + " GB";
} else if (size / ONE_MB > 0) {
displaySize = String.valueOf(size / ONE_MB) + " MB";
} else if (size / ONE_KB > 0) {
displaySize = String.valueOf(size / ONE_KB) + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
public static void main(String[] args) {
System.out.println(Short.MAX_VALUE);
String filename = getName("/home/code4j/es.tar.gz");
System.out.println("bytes.length:"+filename);
// System.out.println(getBottomDir("/home/code4j/IDEAWorkspace/myutils/myutils-db/target/classes/mapper/sqlmapper.js"));;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy