mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add methods
This commit is contained in:
parent
6c62973048
commit
14c343fefd
@ -12,6 +12,9 @@
|
||||
* 【core 】 CaseInsensitiveMap/CamelCaseMap增加toString(issue#636@Github)
|
||||
* 【core 】 XmlUtil多节点改进(issue#I15I0R@Gitee)
|
||||
* 【core 】 Thread.excAsync修正为execAsync(issue#642@Github)
|
||||
* 【core 】 FileUtil.getAbsolutePath修正正则(issue#648@Github)
|
||||
* 【core 】 NetUtil增加getNetworkInterface方法(issue#I15WEL@Gitee)
|
||||
* 【core 】 增加ReflectUtil.getFieldMap方法(issue#I15WJ7@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【extra】 修复SFTP.upload上传失败的问题(issue#I15O40@Gitee)
|
||||
|
@ -1,23 +1,65 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.file.FileCopier;
|
||||
import cn.hutool.core.io.file.FileMode;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.core.io.file.*;
|
||||
import cn.hutool.core.io.file.FileWriter;
|
||||
import cn.hutool.core.io.file.FileReader.ReaderHandler;
|
||||
import cn.hutool.core.io.file.FileWriter;
|
||||
import cn.hutool.core.io.file.LineSeparator;
|
||||
import cn.hutool.core.io.file.Tailer;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.*;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.core.util.ZipUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.Reader;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.CopyOption;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.FileVisitor;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.CRC32;
|
||||
@ -562,8 +604,8 @@ public class FileUtil {
|
||||
if (ArrayUtil.isEmpty(subFiles)) {
|
||||
return 0L;// empty directory
|
||||
}
|
||||
for (int i = 0; i < subFiles.length; i++) {
|
||||
size += size(subFiles[i]);
|
||||
for (File subFile : subFiles) {
|
||||
size += size(subFile);
|
||||
}
|
||||
return size;
|
||||
} else {
|
||||
@ -629,6 +671,7 @@ public class FileUtil {
|
||||
if (false == file.exists()) {
|
||||
mkParentDirs(file);
|
||||
try {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
file.createNewFile();
|
||||
} catch (Exception e) {
|
||||
throw new IORuntimeException(e);
|
||||
@ -672,6 +715,7 @@ public class FileUtil {
|
||||
public static File mkParentDirs(File file) {
|
||||
final File parentFile = file.getParentFile();
|
||||
if (null != parentFile && false == parentFile.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
parentFile.mkdirs();
|
||||
}
|
||||
return parentFile;
|
||||
@ -835,6 +879,7 @@ public class FileUtil {
|
||||
final File[] files = directory.listFiles();
|
||||
if (ArrayUtil.isEmpty(files)) {
|
||||
// 空文件夹则删除之
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
directory.delete();
|
||||
} else {
|
||||
for (File childFile : files) {
|
||||
@ -871,6 +916,7 @@ public class FileUtil {
|
||||
return null;
|
||||
}
|
||||
if (false == dir.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
dir.mkdirs();
|
||||
}
|
||||
return dir;
|
||||
@ -918,7 +964,9 @@ public class FileUtil {
|
||||
try {
|
||||
File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile();
|
||||
if (isReCreat) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
file.delete();
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
file.createNewFile();
|
||||
}
|
||||
return file;
|
||||
@ -1083,6 +1131,7 @@ public class FileUtil {
|
||||
}
|
||||
|
||||
if (isOverride && dest.isFile()) {// 只有目标为文件的情况下覆盖之
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
dest.delete();
|
||||
}
|
||||
|
||||
@ -1238,7 +1287,7 @@ public class FileUtil {
|
||||
}
|
||||
|
||||
// 给定的路径已经是绝对路径了
|
||||
return StrUtil.C_SLASH == path.charAt(0) || path.matches("^[a-zA-Z]:[/\\\\].*");
|
||||
return StrUtil.C_SLASH == path.charAt(0) || path.matches("^[a-zA-Z]:([/\\\\].*)?");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -298,6 +298,32 @@ public class NetUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定名称的网卡信息
|
||||
*
|
||||
* @param name 网络接口名,例如Linux下默认是eth0
|
||||
* @return 网卡,未找到返回<code>null</code>
|
||||
* @since 5.0.7
|
||||
*/
|
||||
public static NetworkInterface getNetworkInterface(String name) {
|
||||
Enumeration<NetworkInterface> networkInterfaces;
|
||||
try {
|
||||
networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
} catch (SocketException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
NetworkInterface netInterface;
|
||||
while(networkInterfaces.hasMoreElements()){
|
||||
netInterface = networkInterfaces.nextElement();
|
||||
if(null != netInterface && name.equals(netInterface.getName())){
|
||||
return netInterface;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本机所有网卡
|
||||
*
|
||||
|
@ -6,14 +6,17 @@ import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
import cn.hutool.core.lang.SimpleCache;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
|
||||
import java.lang.reflect.AccessibleObject;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -133,6 +136,22 @@ public class ReflectUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类中字段名和字段对应的Map,包括其父类中的字段
|
||||
*
|
||||
* @param beanClass 类
|
||||
* @return 字段名和字段对应的Map
|
||||
* @since 5.0.7
|
||||
*/
|
||||
public static Map<String, Field> getFieldMap(Class<?> beanClass){
|
||||
final Field[] fields = getFields(beanClass);
|
||||
final HashMap<String, Field> map = MapUtil.newHashMap(fields.length);
|
||||
for (Field field : fields) {
|
||||
map.put(field.getName(), field);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个类中所有字段列表,包括其父类中的字段
|
||||
*
|
||||
@ -350,12 +369,7 @@ public class ReflectUtil {
|
||||
*/
|
||||
public static List<Method> getPublicMethods(Class<?> clazz, Method... excludeMethods) {
|
||||
final HashSet<Method> excludeMethodSet = CollectionUtil.newHashSet(excludeMethods);
|
||||
return getPublicMethods(clazz, new Filter<Method>() {
|
||||
@Override
|
||||
public boolean accept(Method method) {
|
||||
return false == excludeMethodSet.contains(method);
|
||||
}
|
||||
});
|
||||
return getPublicMethods(clazz, method -> false == excludeMethodSet.contains(method));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -367,12 +381,7 @@ public class ReflectUtil {
|
||||
*/
|
||||
public static List<Method> getPublicMethods(Class<?> clazz, String... excludeMethodNames) {
|
||||
final HashSet<String> excludeMethodNameSet = CollectionUtil.newHashSet(excludeMethodNames);
|
||||
return getPublicMethods(clazz, new Filter<Method>() {
|
||||
@Override
|
||||
public boolean accept(Method method) {
|
||||
return false == excludeMethodNameSet.contains(method.getName());
|
||||
}
|
||||
});
|
||||
return getPublicMethods(clazz, method -> false == excludeMethodNameSet.contains(method.getName()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -770,7 +779,7 @@ public class ReflectUtil {
|
||||
*/
|
||||
public static <T> T invokeWithCheck(Object obj, Method method, Object... args) throws UtilException {
|
||||
final Class<?>[] types = method.getParameterTypes();
|
||||
if (null != types && null != args) {
|
||||
if (null != args) {
|
||||
Assert.isTrue(args.length == types.length, "Params length [{}] is not fit for param length [{}] of method !", args.length, types.length);
|
||||
Class<?> type;
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
|
@ -2401,7 +2401,7 @@ public class StrUtil {
|
||||
Byte dataByte;
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
dataByte = data[i];
|
||||
bytes[i] = (null == dataByte) ? -1 : dataByte.byteValue();
|
||||
bytes[i] = (null == dataByte) ? -1 : dataByte;
|
||||
}
|
||||
|
||||
return str(bytes, charset);
|
||||
@ -4018,8 +4018,8 @@ public class StrUtil {
|
||||
*/
|
||||
public static int totalLength(CharSequence... strs) {
|
||||
int totalLength = 0;
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
totalLength += (null == strs[i] ? 0 : strs[i].length());
|
||||
for (CharSequence str : strs) {
|
||||
totalLength += (null == str ? 0 : str.length());
|
||||
}
|
||||
return totalLength;
|
||||
}
|
||||
|
@ -38,12 +38,12 @@ public class FileUtilTest {
|
||||
String absolutePath2 = FileUtil.getAbsolutePath(absolutePath);
|
||||
Assert.assertNotNull(absolutePath2);
|
||||
Assert.assertEquals(absolutePath, absolutePath2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAbsolutePathTest2() {
|
||||
String path = FileUtil.getAbsolutePath("中文.xml");
|
||||
Assert.assertTrue(path.contains("中文.xml"));
|
||||
|
||||
path = FileUtil.getAbsolutePath("d:");
|
||||
Assert.assertEquals("d:", path);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -134,6 +134,7 @@ public class FileUtilTest {
|
||||
Assert.assertEquals("C:/bar", FileUtil.normalize("C:\\..\\bar"));
|
||||
Assert.assertEquals("bar", FileUtil.normalize("../../bar"));
|
||||
Assert.assertEquals("C:/bar", FileUtil.normalize("/C:/bar"));
|
||||
Assert.assertEquals("C:", FileUtil.normalize("C:"));
|
||||
|
||||
Assert.assertEquals("\\/192.168.1.1/Share/", FileUtil.normalize("\\\\192.168.1.1\\Share\\"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user