提交 bc7e8cf0 编写于 作者: C cmj

see 08/22 log

上级 37a223ae
###
#### 16/08/22 SPUtils将commit改为apply提高效率,将SPUtils改为构造函数法创建,
#### 16/08/22 SPUtils将commit改为apply提高效率,将SPUtils改为构造函数法创建,FileUtils新增查找函数,规范JavaDoc
#### 16/08/21 FileUtils单元测试完毕,修复FileUtils的bug,发布版本1.1.2
#### 16/08/20 更新目录,继续完善FileUtils单元测试,发布版本1.1.1
#### 16/08/19 继续完善FileUtils及单元测试,及其他小修小补(在此感谢vpop的三次Pr)
......
......@@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 23
versionCode 4
versionName "1.1.2"
versionCode 5
versionName "1.1.3"
}
buildTypes {
release {
......
......@@ -17,5 +17,5 @@
#}
#-keep class com.blankj.utilcode.** { *; }
#-keepclassmembers class com.blankj.utilcode.** { *; }
#-keep classmembers class com.blankj.utilcode.** { *; }
#-dontwarn com.blankj.utilcode.**
\ No newline at end of file
......@@ -19,7 +19,7 @@ import java.util.List;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : App相关工具类
* desc : App相关工具类
* </pre>
*/
public class AppUtils {
......
......@@ -16,7 +16,7 @@ import java.io.LineNumberReader;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/1
* desc : 设备相关工具类
* desc : 设备相关工具类
* </pre>
*/
public class DeviceUtils {
......
......@@ -10,11 +10,13 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static com.blankj.utilcode.utils.ConstUtils.KB;
......@@ -24,7 +26,7 @@ import static com.blankj.utilcode.utils.ConstUtils.KB;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/11
* desc : 文件相关工具类
* desc : 文件相关工具类
* </pre>
*/
public class FileUtils {
......@@ -33,20 +35,6 @@ public class FileUtils {
throw new UnsupportedOperationException("u can't fuck me...");
}
/**
* 关闭IO
*
* @param closeable closeable
*/
public static void closeIO(Closeable closeable) {
if (closeable == null) return;
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据文件路径获取文件
*
......@@ -61,7 +49,7 @@ public class FileUtils {
* 判断文件是否存在
*
* @param filePath 文件路径
* @return {@code false}: 存在<br>{@code false}: 不存在
* @return {@code true}: 存在<br>{@code false}: 不存在
*/
public static boolean isFileExists(String filePath) {
return isFileExists(getFileByPath(filePath));
......@@ -71,7 +59,7 @@ public class FileUtils {
* 判断文件是否存在
*
* @param file 文件
* @return {@code false}: 存在<br>{@code false}: 不存在
* @return {@code true}: 存在<br>{@code false}: 不存在
*/
public static boolean isFileExists(File file) {
return file != null && file.exists();
......@@ -81,7 +69,7 @@ public class FileUtils {
* 判断是否是目录
*
* @param dirPath 目录路径
* @return {@code false}: 是<br>{@code false}: 否
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isDir(String dirPath) {
return isDir(getFileByPath(dirPath));
......@@ -423,6 +411,213 @@ public class FileUtils {
return file != null && (!file.exists() || file.isFile() && file.delete());
}
/**
* 获取目录下所有文件
*
* @param dirPath 目录路径
* @param isRecursive 是否递归进子目录
* @return 文件链表
*/
public static List<File> listFilesInDir(String dirPath, boolean isRecursive) {
return listFilesInDir(getFileByPath(dirPath), isRecursive);
}
/**
* 获取目录下所有文件
*
* @param dir 目录
* @param isRecursive 是否递归进子目录
* @return 文件链表
*/
public static List<File> listFilesInDir(File dir, boolean isRecursive) {
if (isRecursive) return listFilesInDir(dir);
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
Collections.addAll(list, files);
return list;
}
/**
* 获取目录下所有文件包括子目录
*
* @param dir 目录
* @return 文件链表
*/
public static List<File> listFilesInDir(File dir) {
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
for (File file : files) {
list.add(file);
if (file.isDirectory()) {
list.addAll(listFilesInDir(file));
}
}
return list;
}
/**
* 获取目录下所有后缀名为suffix的文件
* <p>大小写忽略</p>
*
* @param dirPath 目录路径
* @param isRecursive 是否递归进子目录
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(String dirPath, String suffix, boolean isRecursive) {
return listFilesInDirWithFilter(getFileByPath(dirPath), suffix, isRecursive);
}
/**
* 获取目录下所有后缀名为suffix的文件
* <p>大小写忽略</p>
*
* @param dir 目录
* @param isRecursive 是否递归进子目录
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(File dir, String suffix, boolean isRecursive) {
if (isRecursive) return listFilesInDirWithFilter(dir, suffix);
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
for (File file : files) {
if (file.getName().toUpperCase().endsWith(suffix.toUpperCase())) {
list.add(file);
}
}
return list;
}
/**
* 获取目录下所有后缀名为suffix的文件包括子目录
* <p>大小写忽略</p>
*
* @param dirPath 目录路径
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(String dirPath, String suffix) {
return listFilesInDirWithFilter(getFileByPath(dirPath), suffix);
}
/**
* 获取目录下所有后缀名为suffix的文件包括子目录
* <p>大小写忽略</p>
*
* @param dir 目录
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(File dir, String suffix) {
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
for (File file : files) {
if (file.getName().toUpperCase().endsWith(suffix.toUpperCase())) {
list.add(file);
}
if (file.isDirectory()) {
list.addAll(listFilesInDirWithFilter(file, suffix));
}
}
return list;
}
/**
* 获取目录下所有符合filter的文件
*
* @param dirPath 目录路径
* @param isRecursive 是否递归进子目录
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(String dirPath, FilenameFilter filter, boolean isRecursive) {
return listFilesInDirWithFilter(getFileByPath(dirPath), filter, isRecursive);
}
/**
* 获取目录下所有符合filter的文件
*
* @param dir 目录
* @param isRecursive 是否递归进子目录
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter, boolean isRecursive) {
if (isRecursive) return listFilesInDirWithFilter(dir, filter);
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
for (File file : files) {
if (filter.accept(file.getParentFile(), file.getName())) {
list.add(file);
}
}
return list;
}
/**
* 获取目录下所有符合filter的文件包括子目录
*
* @param dirPath 目录路径
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(String dirPath, FilenameFilter filter) {
return listFilesInDirWithFilter(getFileByPath(dirPath), filter);
}
/**
* 获取目录下所有符合filter的文件包括子目录
*
* @param dir 目录
* @return 文件链表
*/
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter) {
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
for (File file : files) {
if (filter.accept(file.getParentFile(), file.getName())) {
list.add(file);
}
if (file.isDirectory()) {
list.addAll(listFilesInDirWithFilter(file, filter));
}
}
return list;
}
/**
* 获取目录下指定文件名的文件包括子目录
* <p>大小写忽略</p>
*
* @param dirPath 目录路径
* @return 文件链表
*/
public static List<File> searchFileInDir(String dirPath, String fileName) {
return searchFileInDir(getFileByPath(dirPath), fileName);
}
/**
* 获取目录下指定文件名的文件包括子目录
* <p>大小写忽略</p>
*
* @param dir 目录
* @return 文件链表
*/
public static List<File> searchFileInDir(File dir, String fileName) {
if (dir == null || !isDir(dir)) return null;
List<File> list = new ArrayList<>();
File[] files = dir.listFiles();
for (File file : files) {
if (file.getName().toUpperCase().equals(fileName.toUpperCase())) {
list.add(file);
}
if (file.isDirectory()) {
list.addAll(listFilesInDirWithFilter(file, fileName));
}
}
return list;
}
/**
* 将输入流写入文件
*
......@@ -695,10 +890,10 @@ public class FileUtils {
*
* @param size 大小
* @param unit <ul>
* <li>ConstUtils.BYTE:字节</li>
* <li>ConstUtils.KB :千字节</li>
* <li>ConstUtils.MB :兆</li>
* <li>ConstUtils.GB :GB</li>
* <li>{@link ConstUtils#BYTE}: 字节</li>
* <li>{@link ConstUtils#KB} : 千字节</li>
* <li>{@link ConstUtils#MB} : 兆</li>
* <li>{@link ConstUtils#GB} : GB</li>
* </ul>
* @return 大小以unit为单位
*/
......@@ -719,10 +914,10 @@ public class FileUtils {
*
* @param filePath 文件路径
* @param unit <ul>
* <li>ConstUtils.BYTE:字节</li>
* <li>ConstUtils.KB :千字节</li>
* <li>ConstUtils.MB :兆</li>
* <li>ConstUtils.GB :GB</li>
* <li>{@link ConstUtils#BYTE}: 字节</li>
* <li>{@link ConstUtils#KB} : 千字节</li>
* <li>{@link ConstUtils#MB} : 兆</li>
* <li>{@link ConstUtils#GB} : GB</li>
* </ul>
* @return 文件大小以unit为单位
*/
......@@ -736,10 +931,10 @@ public class FileUtils {
*
* @param file 文件
* @param unit <ul>
* <li>ConstUtils.BYTE:字节</li>
* <li>ConstUtils.KB :千字节</li>
* <li>ConstUtils.MB :兆</li>
* <li>ConstUtils.GB :GB</li>
* <li>{@link ConstUtils#BYTE}: 字节</li>
* <li>{@link ConstUtils#KB} : 千字节</li>
* <li>{@link ConstUtils#MB} : 兆</li>
* <li>{@link ConstUtils#GB} : GB</li>
* </ul>
* @return 文件大小以unit为单位
*/
......@@ -749,7 +944,32 @@ public class FileUtils {
}
/**
* 根据全路径获取最长目录
* 关闭IO
*
* @param closeable closeable
*/
public static void closeIO(Closeable closeable) {
if (closeable == null) return;
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取全路径中的最长目录
*
* @param file 文件
* @return filePath最长目录
*/
public static String getDirName(File file) {
if (!isFileExists(file)) return "";
return getDirName(file.getPath());
}
/**
* 获取全路径中的最长目录
*
* @param filePath 文件路径
* @return filePath最长目录
......@@ -761,7 +981,18 @@ public class FileUtils {
}
/**
* 根据全路径获取文件名
* 获取全路径中的文件名
*
* @param file 文件
* @return 文件名
*/
public static String getFileName(File file) {
if (!isFileExists(file)) return "";
return getFileName(file.getPath());
}
/**
* 获取全路径中的文件名
*
* @param filePath 文件路径
* @return 文件名
......@@ -773,10 +1004,21 @@ public class FileUtils {
}
/**
* 根据全路径获取文件名不带拓展名
* 获取全路径中的不带拓展名的文件名
*
* @param file 文件
* @return 不带拓展名的文件名
*/
public static String getFileNameNoExtension(File file) {
if (!isFileExists(file)) return "";
return getFileNameNoExtension(file.getPath());
}
/**
* 获取全路径中的不带拓展名的文件名
*
* @param filePath 文件路径
* @return 文件名不带拓展
* @return 不带拓展名的文件
*/
public static String getFileNameNoExtension(String filePath) {
if (StringUtils.isSpace(filePath)) return filePath;
......@@ -791,8 +1033,20 @@ public class FileUtils {
return filePath.substring(lastSep + 1, lastPoi);
}
/**
* 获取全路径中的文件拓展名
*
* @param file 文件
* @return 文件拓展名
*/
public static String getFileExtension(File file) {
if (!isFileExists(file)) return "";
return getFileExtension(file.getPath());
}
/**
* 根据全路径获取文件拓展名
* 获取全路径中的文件拓展名
*
* @param filePath 文件路径
* @return 文件拓展名
......
......@@ -12,7 +12,7 @@ import android.widget.EditText;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 键盘相关工具类
* desc : 键盘相关工具类
* </pre>
*/
public class KeyboardUtils {
......
......@@ -11,7 +11,7 @@ import android.telephony.TelephonyManager;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 网络相关工具类
* desc : 网络相关工具类
* </pre>
*/
public class NetworkUtils {
......@@ -125,10 +125,10 @@ public class NetworkUtils {
* @param context 上下文
* @return 手机制式
* <ul>
* <li>PHONE_TYPE_NONE : 0 手机制式未知</li>
* <li>PHONE_TYPE_GSM : 1 手机制式为GSM,移动和联通</li>
* <li>PHONE_TYPE_CDMA : 2 手机制式为CDMA,电信</li>
* <li>PHONE_TYPE_SIP : 3</li>
* <li>{@link TelephonyManager#PHONE_TYPE_NONE } : 0 手机制式未知</li>
* <li>{@link TelephonyManager#PHONE_TYPE_GSM } : 1 手机制式为GSM,移动和联通</li>
* <li>{@link TelephonyManager#PHONE_TYPE_CDMA } : 2 手机制式为CDMA,电信</li>
* <li>{@link TelephonyManager#PHONE_TYPE_SIP } : 3</li>
* </ul>
*/
public static int getPhoneType(Context context) {
......@@ -145,12 +145,12 @@ public class NetworkUtils {
* @param context 上下文
* @return 网络类型
* <ul>
* <li>NETWORK_WIFI = 1;</li>
* <li>NETWORK_4G = 4;</li>
* <li>NETWORK_3G = 3;</li>
* <li>NETWORK_2G = 2;</li>
* <li>NETWORK_UNKNOWN = 5;</li>
* <li>NETWORK_NO = -1;</li>
* <li>{@link #NETWORK_WIFI } = 1;</li>
* <li>{@link #NETWORK_4G } = 4;</li>
* <li>{@link #NETWORK_3G } = 3;</li>
* <li>{@link #NETWORK_2G } = 2;</li>
* <li>{@link #NETWORK_UNKNOWN} = 5;</li>
* <li>{@link #NETWORK_NO } = -1;</li>
* </ul>
*/
public static int getNetWorkType(Context context) {
......
......@@ -24,7 +24,7 @@ import java.util.List;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 手机相关工具类
* desc : 手机相关工具类
* </pre>
*/
public class PhoneUtils {
......
......@@ -9,7 +9,7 @@ import static com.blankj.utilcode.utils.ConstUtils.*;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 正则相关工具类
* desc : 正则相关工具类
* </pre>
*/
public class RegularUtils {
......
......@@ -9,7 +9,7 @@ import java.io.File;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/11
* desc : SD卡相关工具类
* desc : SD卡相关工具类
* </pre>
*/
public class SDCardUtils {
......
......@@ -186,7 +186,7 @@ public class SPUtils {
}
/**
* 获取sp中所有键值对
* 获取SP中所有键值对
*
* @return Map对象
*/
......@@ -195,7 +195,7 @@ public class SPUtils {
}
/**
* 从sp中移除该key
* 从SP中移除该key
*
* @param key 键
*/
......@@ -204,7 +204,7 @@ public class SPUtils {
}
/**
* 判断sp中是否存在该key
* 判断SP中是否存在该key
*
* @param key 键
* @return {@code true}: 存在<br>{@code false}: 不存在
......@@ -214,7 +214,7 @@ public class SPUtils {
}
/**
* 清除所有数据
* 清除SP中所有数据
*/
public void clear() {
editor.clear().apply();
......
......@@ -20,7 +20,7 @@ import java.lang.reflect.Method;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 屏幕相关工具类
* desc : 屏幕相关工具类
* </pre>
*/
public class ScreenUtils {
......
......@@ -11,7 +11,7 @@ import java.util.List;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/7
* desc : Shell操作工具类
* desc : Shell相关工具类
* </pre>
*/
public class ShellUtils {
......
......@@ -11,7 +11,7 @@ import android.view.View;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 尺寸相关工具类
* desc : 尺寸相关工具类
* </pre>
*/
public class SizeUtils {
......
......@@ -11,7 +11,7 @@ import java.util.Locale;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 时间相关工具类
* desc : 时间相关工具类
* </pre>
*/
public class TimeUtils {
......@@ -300,11 +300,11 @@ public class TimeUtils {
*
* @param milliseconds 毫秒时间戳
* @param unit <ul>
* <li>ConstUtils.MSEC:毫秒</li>
* <li>ConstUtils.SEC :秒</li>
* <li>ConstUtils.MIN :分</li>
* <li>ConstUtils.HOUR:小时</li>
* <li>ConstUtils.DAY :天</li>
* <li>{@link ConstUtils#MSEC}: 毫秒</li>
* <li>{@link ConstUtils#SEC }: 秒</li>
* <li>{@link ConstUtils#MIN }: 分</li>
* <li>{@link ConstUtils#HOUR}: 小时</li>
* <li>{@link ConstUtils#DAY }: 天</li>
* </ul>
* @return unit时间戳
*/
......@@ -327,11 +327,11 @@ public class TimeUtils {
* @param time0 时间字符串1
* @param time1 时间字符串2
* @param unit <ul>
* <li>ConstUtils.MSEC:毫秒</li>
* <li>ConstUtils.SEC :秒</li>
* <li>ConstUtils.MIN :分</li>
* <li>ConstUtils.HOUR:小时</li>
* <li>ConstUtils.DAY :天</li>
* <li>{@link ConstUtils#MSEC}: 毫秒</li>
* <li>{@link ConstUtils#SEC }: 秒</li>
* <li>{@link ConstUtils#MIN }: 分</li>
* <li>{@link ConstUtils#HOUR}: 小时</li>
* <li>{@link ConstUtils#DAY }: 天</li>
* </ul>
* @return unit时间戳
*/
......@@ -346,11 +346,11 @@ public class TimeUtils {
* @param time0 时间字符串1
* @param time1 时间字符串2
* @param unit <ul>
* <li>ConstUtils.MSEC:毫秒</li>
* <li>ConstUtils.SEC :秒</li>
* <li>ConstUtils.MIN :分</li>
* <li>ConstUtils.HOUR:小时</li>
* <li>ConstUtils.DAY :天</li>
* <li>{@link ConstUtils#MSEC}: 毫秒</li>
* <li>{@link ConstUtils#SEC }: 秒</li>
* <li>{@link ConstUtils#MIN }: 分</li>
* <li>{@link ConstUtils#HOUR}: 小时</li>
* <li>{@link ConstUtils#DAY }: 天</li>
* </ul>
* @param format 时间格式
* @return unit时间戳
......@@ -367,11 +367,11 @@ public class TimeUtils {
* @param time0 Date类型时间1
* @param time1 Date类型时间2
* @param unit <ul>
* <li>ConstUtils.MSEC:毫秒</li>
* <li>ConstUtils.SEC :秒</li>
* <li>ConstUtils.MIN :分</li>
* <li>ConstUtils.HOUR:小时</li>
* <li>ConstUtils.DAY :天</li>
* <li>{@link ConstUtils#MSEC}: 毫秒</li>
* <li>{@link ConstUtils#SEC }: 秒</li>
* <li>{@link ConstUtils#MIN }: 分</li>
* <li>{@link ConstUtils#HOUR}: 小时</li>
* <li>{@link ConstUtils#DAY }: 天</li>
* </ul>
* @return unit时间戳
*/
......@@ -426,11 +426,11 @@ public class TimeUtils {
*
* @param time 时间字符串
* @param unit <ul>
* <li>ConstUtils.MSEC:毫秒</li>
* <li>ConstUtils.SEC :秒</li>
* <li>ConstUtils.MIN :分</li>
* <li>ConstUtils.HOUR:小时</li>
* <li>ConstUtils.DAY :天</li>
* <li>{@link ConstUtils#MSEC}:毫秒</li>
* <li>{@link ConstUtils#SEC }:秒</li>
* <li>{@link ConstUtils#MIN }:分</li>
* <li>{@link ConstUtils#HOUR}:小时</li>
* <li>{@link ConstUtils#DAY }:天</li>
* </ul>
* @return unit时间戳
*/
......@@ -444,11 +444,11 @@ public class TimeUtils {
*
* @param time 时间字符串
* @param unit <ul>
* <li>ConstUtils.MSEC:毫秒</li>
* <li>ConstUtils.SEC :秒</li>
* <li>ConstUtils.MIN :分</li>
* <li>ConstUtils.HOUR:小时</li>
* <li>ConstUtils.DAY :天</li>
* <li>{@link ConstUtils#MSEC}: 毫秒</li>
* <li>{@link ConstUtils#SEC }: 秒</li>
* <li>{@link ConstUtils#MIN }: 分</li>
* <li>{@link ConstUtils#HOUR}: 小时</li>
* <li>{@link ConstUtils#DAY }: 天</li>
* </ul>
* @param format 时间格式
* @return unit时间戳
......@@ -463,11 +463,11 @@ public class TimeUtils {
*
* @param time Date类型时间
* @param unit <ul>
* <li>ConstUtils.MSEC:毫秒</li>
* <li>ConstUtils.SEC :秒</li>
* <li>ConstUtils.MIN :分</li>
* <li>ConstUtils.HOUR:小时</li>
* <li>ConstUtils.DAY :天</li>
* <li>{@link ConstUtils#MSEC}: 毫秒</li>
* <li>{@link ConstUtils#SEC }: 秒</li>
* <li>{@link ConstUtils#MIN }: 分</li>
* <li>{@link ConstUtils#HOUR}: 小时</li>
* <li>{@link ConstUtils#DAY }: 天</li>
* </ul>
* @return unit时间戳
*/
......
......@@ -12,7 +12,7 @@ import java.util.List;
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/2
* desc : 未归类工具类
* desc : 未归类工具类
* </pre>
*/
public class UnclassifiedUtils {
......
......@@ -2,7 +2,9 @@ package com.blankj.utilcode.utils;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import static com.blankj.utilcode.utils.FileUtils.*;
import static com.blankj.utilcode.utils.TestUtils.BASEPATH;
......@@ -108,6 +110,32 @@ public class FileUtilsTest {
assertThat(deleteFile(path + "del.txt")).isTrue();
}
@Test
public void testListFilesInDir() throws Exception {
System.out.println(listFilesInDir(path, false).toString());
System.out.println(listFilesInDir(path, true).toString());
}
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("k.txt");
}
};
@Test
public void testListFilesInDirWithFiltere() throws Exception {
System.out.println(listFilesInDirWithFilter(path, "k.txt", false).toString());
System.out.println(listFilesInDirWithFilter(path, "k.txt", true).toString());
System.out.println(listFilesInDirWithFilter(path, filter, false).toString());
System.out.println(listFilesInDirWithFilter(path, filter, true).toString());
}
@Test
public void testSearchFile() throws Exception {
System.out.println(searchFileInDir(path, "GBK.txt").toString());
System.out.println(searchFileInDir(path, "child").toString());
}
@Test
public void testWriteFileFromIS() throws Exception {
assertThat(writeFileFromIS(path + "NEW.txt", new FileInputStream(path + "UTF8.txt"), false))
......@@ -139,6 +167,7 @@ public class FileUtilsTest {
public void testReadFile2List() throws Exception {
System.out.println(readFile2List(path + "UTF8.txt", "").toString());
System.out.println(readFile2List(path + "UTF8.txt", "UTF-8").toString());
System.out.println(readFile2List(path + "UTF8.txt", 2, 5, "UTF-8").toString());
System.out.println(readFile2List(path + "UTF8.txt", "GBK").toString());
}
......@@ -161,21 +190,26 @@ public class FileUtilsTest {
@Test
public void testGetDirName() throws Exception {
assertThat(getDirName(new File(path + "UTF8.txt"))).isEqualTo(path);
assertThat(getDirName(path + "UTF8.txt")).isEqualTo(path);
}
@Test
public void testGetFileName() throws Exception {
assertThat(getFileName(new File(path + "UTF8.txt"))).isEqualTo("UTF8.txt");
assertThat(getFileName(path + "UTF8.txt")).isEqualTo("UTF8.txt");
}
@Test
public void testGetFileNameNoExtension() throws Exception {
assertThat(getFileNameNoExtension(new File(path + "UTF8.txt"))).isEqualTo("UTF8");
assertThat(getFileNameNoExtension(path + "UTF8.txt")).isEqualTo("UTF8");
}
@Test
public void testGetFileExtension() throws Exception {
assertThat(getFileExtension(new File(path + "UTF8.txt"))).isEqualTo("txt");
assertThat(getFileExtension(path + "UTF8.txt")).isEqualTo("txt");
System.out.println(new File(path).getName().endsWith("file"));
}
}
\ No newline at end of file
package com.blankj.utilcode.utils;
import android.content.Context;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.io.File;
/**
......@@ -10,6 +17,8 @@ import java.io.File;
* desc : 单元测试工具类
* </pre>
*/
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class TestUtils {
private TestUtils() {
......@@ -20,4 +29,8 @@ public class TestUtils {
public static final String BASEPATH = System.getProperty("user.dir")
+ SEP + "src" + SEP + "test" + SEP + "res" + SEP;
public static Context getContext() {
return ShadowApplication.getInstance().getApplicationContext();
}
}
GBK
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":utilcode" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.blankj" external.system.module.version="1.1.1" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":utilcode" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.blankj" external.system.module.version="1.1.2" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
......@@ -101,42 +101,42 @@
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 23 Platform (2)" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="TEST" name="backport-util-concurrent-3.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-ant-tasks-2.1.3" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="backport-util-concurrent-3.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-profile-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="ant-1.8.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xercesMinimal-1.9.6.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-settings-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="hamcrest-library-1.3" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="ant-launcher-1.8.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="accessibility-test-framework-2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-commons-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="accessibility-test-framework-2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xpp3_min-1.1.4c" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-http-lightweight-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="nekohtml-1.9.6.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-file-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-error-diagnostics-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-repository-metadata-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-provider-api-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xstream-1.4.8" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-provider-api-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-util-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="bcprov-jdk16-1.46" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-http-shared-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-model-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xmlpull-1.1.3.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-artifact-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="error_prone_annotations-2.0.8" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-artifact-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="guava-19.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="truth-0.29" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="robolectric-annotations-3.1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="icu4j-53.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="robolectric-utils-3.1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="robolectric-3.1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="sqlite4java-0.282" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-tree-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="sqlite4java-0.282" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-utils-1.5.15" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-plugin-registry-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-analysis-5.0.1" level="project" />
......@@ -145,11 +145,11 @@
<orderEntry type="library" exported="" scope="TEST" name="maven-project-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="vtd-xml-2.11" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="hamcrest-core-1.3" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-container-default-1.0-alpha-9-stable-1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="junit-4.12" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="shadows-core-v23-3.1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-interpolation-1.11" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="classworlds-1.1-alpha-2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-container-default-1.0-alpha-9-stable-1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-artifact-manager-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="classworlds-1.1-alpha-2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-interpolation-1.11" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="shadows-core-v23-3.1.2" level="project" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册