readFile2List(String filePath, int start, int end, String
+ charsetName) {
return readFile2List(getFileByPath(filePath), start, end, charsetName);
}
@@ -653,7 +671,12 @@ public class FileUtils {
BufferedReader reader = null;
try {
sb = new StringBuilder();
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
+ if (StringUtils.isSpace(charsetName)) {
+ reader = new BufferedReader(new FileReader(file));
+ } else {
+ reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
+ charsetName));
+ }
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java
index 2f2cdf0b85ca1991eecf47773bce7895116e3e97..6a863737bb2de0af13e9e0b168529f94deeb8fcb 100644
--- a/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java
+++ b/utilcode/src/test/java/com/blankj/utilcode/utils/EncryptUtilsTest.java
@@ -29,8 +29,10 @@ public class EncryptUtilsTest {
String blankjSHA1 = "C606ACCB1FEB669E19D080ADDDDBB8E6CDA5F43C";
String blankjSHA224 = "F4C5C0E8CF56CAC4D06DB6B523F67621859A9D79BDA4B2AC03097D5F";
String blankjSHA256 = "8BD80AE90DFBA112786367BEBDDEE60A638EF5B82682EDF8F3D3CA8E6BFEF648";
- String blankjSHA384 = "BF831E5221FC108D6A72ACB888BA3EB0C030A5F01BA2F739856BE70681D86F992B85E0D461101C74BAEDA895BD422557";
- String blankjSHA512 = "D59D31067F614ED3586F85A31FEFDB7F33096316DA26EBE0FF440B241C8560D96650F100D78C512560C976949EFA89CB5D5589DCF68C7FAADE98F03BCFEC2B45";
+ String blankjSHA384 =
+ "BF831E5221FC108D6A72ACB888BA3EB0C030A5F01BA2F739856BE70681D86F992B85E0D461101C74BAEDA895BD422557";
+ String blankjSHA512 =
+ "D59D31067F614ED3586F85A31FEFDB7F33096316DA26EBE0FF440B241C8560D96650F100D78C512560C976949EFA89CB5D5589DCF68C7FAADE98F03BCFEC2B45";
@Test
public void testEncryptMD2() throws Exception {
@@ -92,14 +94,16 @@ public class EncryptUtilsTest {
public void testEncryptDES() throws Exception {
assertThat(encryptDES(bytesDataDES, bytesKeyDES)).isEqualTo(bytesResDES);
assertThat(encryptDES2HexString(bytesDataDES, bytesKeyDES)).isEqualTo(resDES);
- assertThat(encryptDES2Base64(bytesDataDES, bytesKeyDES)).isEqualTo(base64Encode(bytesResDES));
+ assertThat(encryptDES2Base64(bytesDataDES, bytesKeyDES)).isEqualTo(base64Encode
+ (bytesResDES));
}
@Test
public void testDecryptDES() throws Exception {
assertThat(decryptDES(bytesResDES, bytesKeyDES)).isEqualTo(bytesDataDES);
assertThat(decryptHexStringDES(resDES, bytesKeyDES)).isEqualTo(bytesDataDES);
- assertThat(decryptBase64DES(base64Encode(bytesResDES), bytesKeyDES)).isEqualTo(bytesDataDES);
+ assertThat(decryptBase64DES(base64Encode(bytesResDES), bytesKeyDES)).isEqualTo
+ (bytesDataDES);
}
String data3DES = "1111111111111111";
@@ -113,14 +117,16 @@ public class EncryptUtilsTest {
public void testEncrypt3DES() throws Exception {
assertThat(encrypt3DES(bytesDataDES3, bytesKeyDES3)).isEqualTo(bytesResDES3);
assertThat(encrypt3DES2HexString(bytesDataDES3, bytesKeyDES3)).isEqualTo(res3DES);
- assertThat(encrypt3DES2Base64(bytesDataDES3, bytesKeyDES3)).isEqualTo(base64Encode(bytesResDES3));
+ assertThat(encrypt3DES2Base64(bytesDataDES3, bytesKeyDES3)).isEqualTo(base64Encode
+ (bytesResDES3));
}
@Test
public void testDecrypt3DES() throws Exception {
assertThat(decrypt3DES(bytesResDES3, bytesKeyDES3)).isEqualTo(bytesDataDES3);
assertThat(decryptHexString3DES(res3DES, bytesKeyDES3)).isEqualTo(bytesDataDES3);
- assertThat(decryptBase64_3DES(base64Encode(bytesResDES3), bytesKeyDES3)).isEqualTo(bytesDataDES3);
+ assertThat(decryptBase64_3DES(base64Encode(bytesResDES3), bytesKeyDES3)).isEqualTo
+ (bytesDataDES3);
}
String dataAES = "11111111111111111111111111111111";
@@ -134,21 +140,23 @@ public class EncryptUtilsTest {
public void testEncryptAES() throws Exception {
assertThat(encryptAES(bytesDataAES, bytesKeyAES)).isEqualTo(bytesResAES);
assertThat(encryptAES2HexString(bytesDataAES, bytesKeyAES)).isEqualTo(resAES);
- assertThat(encryptAES2Base64(bytesDataAES, bytesKeyAES)).isEqualTo(base64Encode(bytesResAES));
+ assertThat(encryptAES2Base64(bytesDataAES, bytesKeyAES)).isEqualTo(base64Encode
+ (bytesResAES));
}
@Test
public void testDecryptAES() throws Exception {
assertThat(decryptAES(bytesResAES, bytesKeyAES)).isEqualTo(bytesDataAES);
assertThat(decryptHexStringAES(resAES, bytesKeyAES)).isEqualTo(bytesDataAES);
- assertThat(decryptBase64AES(base64Encode(bytesResAES), bytesKeyAES)).isEqualTo(bytesDataAES);
+ assertThat(decryptBase64AES(base64Encode(bytesResAES), bytesKeyAES)).isEqualTo
+ (bytesDataAES);
}
- String path = System.getProperty("user.dir") + "\\src\\test\\res\\";
+ String path = TestUtils.BASEPATH + "encrypt" + TestUtils.SEP;
String md5 = "7F138A09169B250E9DCB378140907378";
@Test
public void testEncryptMD5File() throws Exception {
- assertThat(encryptMD5File2String(new File(path + File.separator + "MD5.txt"))).isEqualTo(md5);
+ assertThat(encryptMD5File2String(new File(path + "MD5.txt"))).isEqualTo(md5);
}
}
\ No newline at end of file
diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java
index d14fd7e6a9632b2cfeccfb004cffe3704df05132..97d93666762e9c8a66883910abebaf7f04134291 100644
--- a/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java
+++ b/utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java
@@ -1,55 +1,181 @@
-package com.blankj.utilcode.utils;
-
-import org.junit.Test;
-
-import static com.blankj.utilcode.utils.FileUtils.*;
-import static com.google.common.truth.Truth.assertThat;
-
-/**
- *
- * author: Blankj
- * blog : http://blankj.com
- * time : 2016/8/19
- * desc : FileUtils单元测试
- *
- */
-public class FileUtilsTest {
-
- String path = System.getProperty("user.dir") + "\\src\\test\\res\\";
-
- @Test
- public void testIsFileExists() throws Exception {
- assertThat(isFileExists(path + "UTF8.txt")).isTrue();
- assertThat(isFileExists(path + "UTF8")).isFalse();
- }
-
- @Test
- public void testIsDirectory() throws Exception {
- assertThat(isDir(path + "UTF8.txt")).isFalse();
- assertThat(isDir(path)).isTrue();
- }
-
- @Test
- public void testIsFile() throws Exception {
- assertThat(isFile(path + "UTF8.txt")).isTrue();
- assertThat(isFile(path)).isFalse();
- }
-
- @Test
- public void testCreateOrExistsDir() throws Exception {
- assertThat(createOrExistsDir(path + "new Dir")).isTrue();
- assertThat(createOrExistsDir(path)).isTrue();
- }
-
- @Test
- public void testCreateOrExistsFile() throws Exception {
- assertThat(createOrExistsFile(path + "new Dir")).isTrue();
- assertThat(createOrExistsFile(path)).isFalse();
- }
-
- @Test
- public void testCreateFileByDeleteOldFile() throws Exception {
- assertThat(createFileByDeleteOldFile(path + "new Dir")).isTrue();
- assertThat(createFileByDeleteOldFile(path)).isFalse();
- }
+package com.blankj.utilcode.utils;
+
+import org.junit.Test;
+
+import java.io.FileInputStream;
+
+import static com.blankj.utilcode.utils.FileUtils.*;
+import static com.blankj.utilcode.utils.TestUtils.BASEPATH;
+import static com.blankj.utilcode.utils.TestUtils.SEP;
+import static com.google.common.truth.Truth.assertThat;
+
+/**
+ *
+ * author: Blankj
+ * blog : http://blankj.com
+ * time : 2016/8/19
+ * desc : FileUtils单元测试
+ *
+ */
+public class FileUtilsTest {
+
+
+ String path = BASEPATH + "file" + SEP;
+
+ @Test
+ public void testGetFileByPath() throws Exception {
+ assertThat(getFileByPath(" ")).isNull();
+ assertThat(getFileByPath("c:")).isNotNull();
+ }
+
+ @Test
+ public void testIsFileExists() throws Exception {
+ assertThat(isFileExists(path + "UTF8.txt")).isTrue();
+ assertThat(isFileExists(path + "UTF8")).isFalse();
+ }
+
+ @Test
+ public void testIsDir() throws Exception {
+ assertThat(isDir(path + "UTF8.txt")).isFalse();
+ assertThat(isDir(path)).isTrue();
+ }
+
+ @Test
+ public void testIsFile() throws Exception {
+ assertThat(isFile(path + "UTF8.txt")).isTrue();
+ assertThat(isFile(path)).isFalse();
+ }
+
+ @Test
+ public void testCreateOrExistsDir() throws Exception {
+ assertThat(createOrExistsDir(path + "new Dir")).isTrue();
+ assertThat(createOrExistsDir(path)).isTrue();
+ }
+
+ @Test
+ public void testCreateOrExistsFile() throws Exception {
+ assertThat(createOrExistsFile(path + "new File")).isTrue();
+ assertThat(createOrExistsFile(path)).isFalse();
+ }
+
+ @Test
+ public void testCreateFileByDeleteOldFile() throws Exception {
+ assertThat(createFileByDeleteOldFile(path + "new File")).isTrue();
+ assertThat(createFileByDeleteOldFile(path)).isFalse();
+ }
+
+ String path1 = BASEPATH + "file1" + SEP;
+
+ @Test
+ public void testCopyDir() throws Exception {
+ assertThat(copyDir(path, path)).isFalse();
+ assertThat(copyDir(path, path + "new Dir")).isFalse();
+ assertThat(copyDir(path, path1)).isTrue();
+ }
+
+ @Test
+ public void testCopyFile() throws Exception {
+ assertThat(copyFile(path + "GBK.txt", path + "GBK.txt")).isFalse();
+ assertThat(copyFile(path + "GBK.txt", path + "new Dir" + SEP + "GBK.txt")).isTrue();
+ assertThat(copyFile(path + "GBK.txt", path1 + "GBK.txt")).isTrue();
+ }
+
+ @Test
+ public void testMoveDir() throws Exception {
+ assertThat(moveDir(path, path)).isFalse();
+ assertThat(moveDir(path, path + "new Dir")).isFalse();
+ assertThat(moveDir(path, path1)).isTrue();
+ assertThat(moveDir(path1, path)).isTrue();
+ }
+
+ @Test
+ public void testMoveFile() throws Exception {
+ assertThat(moveFile(path + "GBK.txt", path + "GBK.txt")).isFalse();
+ assertThat(moveFile(path + "GBK.txt", path1 + "GBK.txt")).isTrue();
+ assertThat(moveFile(path1 + "GBK.txt", path + "GBK.txt")).isTrue();
+ }
+
+ @Test
+ public void testDeleteDir() throws Exception {
+ assertThat(deleteDir(path + "GBK.txt")).isFalse();
+ assertThat(deleteDir(path + "del")).isTrue();
+ }
+
+ @Test
+ public void testDeleteFile() throws Exception {
+ assertThat(deleteFile(path)).isFalse();
+ assertThat(deleteFile(path + "GBK1.txt")).isTrue();
+ assertThat(deleteFile(path + "del.txt")).isTrue();
+ }
+
+ @Test
+ public void testWriteFileFromIS() throws Exception {
+ assertThat(writeFileFromIS(path + "NEW.txt", new FileInputStream(path + "UTF8.txt"), false))
+ .isTrue();
+ assertThat(writeFileFromIS(path + "NEW.txt", new FileInputStream(path + "UTF8.txt"), true))
+ .isTrue();
+ }
+
+ @Test
+ public void testWriteFileFromString() throws Exception {
+ assertThat(writeFileFromString(path + "NEW.txt", "这是新的", false)).isTrue();
+ assertThat(writeFileFromString(path + "NEW.txt", "\r\n这是追加的", true)).isTrue();
+ }
+
+ @Test
+ public void testGetFileCharsetSimple() throws Exception {
+ assertThat(getFileCharsetSimple(path + "GBK.txt")).isEqualTo("GBK");
+ assertThat(getFileCharsetSimple(path + "Unicode.txt")).isEqualTo("Unicode");
+ assertThat(getFileCharsetSimple(path + "UTF8.txt")).isEqualTo("UTF-8");
+ assertThat(getFileCharsetSimple(path + "UTF16BE.txt")).isEqualTo("UTF-16BE");
+ }
+
+ @Test
+ public void testGetFileLines() throws Exception {
+ assertThat(getFileLines(path + "UTF8.txt")).isEqualTo(7);
+ }
+
+ @Test
+ 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", "GBK").toString());
+ }
+
+ @Test
+ public void testReadFile2SB() throws Exception {
+ System.out.println(readFile2SB(path + "UTF8.txt", "").toString());
+ System.out.println(readFile2SB(path + "UTF8.txt", "UTF-8").toString());
+ System.out.println(readFile2SB(path + "UTF8.txt", "GBK").toString());
+ }
+
+ @Test
+ public void testByte2Unit() throws Exception {
+ assertThat(byte2Unit(ConstUtils.GB, ConstUtils.MB) - 1024).isWithin(0.001);
+ }
+
+ @Test
+ public void testGetFileSize() throws Exception {
+ assertThat(getFileSize(path + "UTF8.txt", ConstUtils.BYTE) - 25).isWithin(0.001);
+ }
+
+ @Test
+ public void testGetDirName() throws Exception {
+ assertThat(getDirName(path + "UTF8.txt")).isEqualTo(path);
+ }
+
+ @Test
+ public void testGetFileName() throws Exception {
+ assertThat(getFileName(path + "UTF8.txt")).isEqualTo("UTF8.txt");
+ }
+
+ @Test
+ public void testGetFileNameNoExtension() throws Exception {
+ assertThat(getFileNameNoExtension(path + "UTF8.txt")).isEqualTo("UTF8");
+ }
+
+ @Test
+ public void testGetFileExtension() throws Exception {
+ assertThat(getFileExtension(path + "UTF8.txt")).isEqualTo("txt");
+ }
}
\ No newline at end of file
diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java b/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..dcde22e7bc33d6a13c7ae1dc5aefc1f98e5949b2
--- /dev/null
+++ b/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java
@@ -0,0 +1,23 @@
+package com.blankj.utilcode.utils;
+
+import java.io.File;
+
+/**
+ *
+ * author: Blankj
+ * blog : http://blankj.com
+ * time : 2016/8/21
+ * desc : 单元测试工具类
+ *
+ */
+public class TestUtils {
+
+ private TestUtils() {
+ throw new UnsupportedOperationException("u can't fuck me...");
+ }
+
+ public static final char SEP = File.separatorChar;
+
+ public static final String BASEPATH = System.getProperty("user.dir")
+ + SEP + "src" + SEP + "test" + SEP + "res" + SEP;
+}
diff --git a/utilcode/src/test/res/MD5.txt b/utilcode/src/test/res/encrypt/MD5.txt
similarity index 100%
rename from utilcode/src/test/res/MD5.txt
rename to utilcode/src/test/res/encrypt/MD5.txt
diff --git a/utilcode/src/test/res/GBK.txt b/utilcode/src/test/res/file/GBK.txt
similarity index 100%
rename from utilcode/src/test/res/GBK.txt
rename to utilcode/src/test/res/file/GBK.txt
diff --git a/utilcode/src/test/res/UTF16BE.txt b/utilcode/src/test/res/file/UTF16BE.txt
similarity index 100%
rename from utilcode/src/test/res/UTF16BE.txt
rename to utilcode/src/test/res/file/UTF16BE.txt
diff --git a/utilcode/src/test/res/UTF8.txt b/utilcode/src/test/res/file/UTF8.txt
similarity index 100%
rename from utilcode/src/test/res/UTF8.txt
rename to utilcode/src/test/res/file/UTF8.txt
diff --git a/utilcode/src/test/res/Unicode.txt b/utilcode/src/test/res/file/Unicode.txt
similarity index 100%
rename from utilcode/src/test/res/Unicode.txt
rename to utilcode/src/test/res/file/Unicode.txt
diff --git a/utilcode/src/test/res/new Dir b/utilcode/src/test/res/new Dir
deleted file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000
diff --git a/utilcode/utilcode.iml b/utilcode/utilcode.iml
index cb03c4980a69c6e8a12821817a8a7c631b0927b1..01c5fe7a6c51772f4c58644cfad729da68f11533 100644
--- a/utilcode/utilcode.iml
+++ b/utilcode/utilcode.iml
@@ -65,14 +65,6 @@
-
-
-
-
-
-
-
-
@@ -81,6 +73,14 @@
+
+
+
+
+
+
+
+