Skip to content

Commit 104c123

Browse files
committed
see 05/23 log
1 parent 76386de commit 104c123

File tree

3 files changed

+125
-13
lines changed

3 files changed

+125
-13
lines changed

app/src/main/java/com/blankj/androidutilcode/activity/LogActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private void updateAbout(int args) {
249249
}
250250

251251
private String getDir() {
252-
return mBuilder.toString().split(System.getProperty("line.separator"))[4].substring(5);
252+
return mBuilder.toString().split(System.getProperty("line.separator"))[5].substring(5);
253253
}
254254

255255
@Override

utilcode/src/main/java/com/blankj/utilcode/util/FileUtils.java

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
import java.io.FileInputStream;
1414
import java.io.FileNotFoundException;
1515
import java.io.FileOutputStream;
16-
import java.io.FileReader;
1716
import java.io.FileWriter;
1817
import java.io.FilenameFilter;
1918
import java.io.IOException;
2019
import java.io.InputStream;
2120
import java.io.InputStreamReader;
2221
import java.io.OutputStream;
22+
import java.io.RandomAccessFile;
23+
import java.nio.ByteBuffer;
24+
import java.nio.MappedByteBuffer;
25+
import java.nio.channels.FileChannel;
2326
import java.security.DigestInputStream;
2427
import java.security.MessageDigest;
2528
import java.security.NoSuchAlgorithmException;
@@ -31,7 +34,7 @@
3134
* <pre>
3235
* author: Blankj
3336
* blog : http://blankj.com
34-
* time : 2016/08/11
37+
* time : 2016/05/03
3538
* desc : 文件相关工具类
3639
* </pre>
3740
*/
@@ -41,6 +44,8 @@ private FileUtils() {
4144
throw new UnsupportedOperationException("u can't instantiate me...");
4245
}
4346

47+
private static final String LINE_SEP = System.getProperty("line.separator");
48+
4449
/**
4550
* 根据文件路径获取文件
4651
*
@@ -772,6 +777,42 @@ public static boolean writeFileFromIS(File file, InputStream is, boolean append)
772777
}
773778
}
774779

780+
/**
781+
* 将字节数组写入文件
782+
*
783+
* @param filePath 文件路径
784+
* @param bytes 字节数组
785+
* @param append 是否追加在文件末
786+
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
787+
*/
788+
public static boolean writeFileFromBytes(String filePath, byte[] bytes, boolean append) {
789+
return writeFileFromBytes(getFileByPath(filePath), bytes, append);
790+
}
791+
792+
/**
793+
* 将字节数组写入文件
794+
*
795+
* @param file 文件
796+
* @param bytes 字节数组
797+
* @param append 是否追加在文件末
798+
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
799+
*/
800+
public static boolean writeFileFromBytes(File file, byte[] bytes, boolean append) {
801+
if (file == null || bytes == null) return false;
802+
if (!createOrExistsFile(file)) return false;
803+
BufferedOutputStream bos = null;
804+
try {
805+
bos = new BufferedOutputStream(new FileOutputStream(file, append));
806+
bos.write(bytes);
807+
return true;
808+
} catch (IOException e) {
809+
e.printStackTrace();
810+
return false;
811+
} finally {
812+
CloseUtils.closeIO(bos);
813+
}
814+
}
815+
775816
/**
776817
* 将字符串写入文件
777818
*
@@ -862,7 +903,7 @@ public static List<String> readFile2List(File file, int st, int end, String char
862903
int curLine = 1;
863904
List<String> list = new ArrayList<>();
864905
if (isSpace(charsetName)) {
865-
reader = new BufferedReader(new FileReader(file));
906+
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
866907
} else {
867908
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
868909
}
@@ -910,10 +951,10 @@ public static String readFile2String(File file, String charsetName) {
910951
}
911952
String line;
912953
while ((line = reader.readLine()) != null) {
913-
sb.append(line).append("\r\n");// windows系统换行为\r\n,Linux为\n
954+
sb.append(line).append(LINE_SEP);
914955
}
915-
// 要去除最后的换行符
916-
return sb.delete(sb.length() - 2, sb.length()).toString();
956+
// delete the last line separator
957+
return sb.delete(sb.length() - LINE_SEP.length(), sb.length()).toString();
917958
} catch (IOException e) {
918959
e.printStackTrace();
919960
return null;
@@ -948,6 +989,77 @@ public static byte[] readFile2Bytes(File file) {
948989
}
949990
}
950991

992+
/**
993+
* 读取文件到字符数组中
994+
*
995+
* @param filePath 文件路径
996+
* @return 字符数组
997+
*/
998+
public static byte[] readFile2BytesByChannel(String filePath) {
999+
return readFile2BytesByChannel(getFileByPath(filePath));
1000+
}
1001+
1002+
/**
1003+
* 读取文件到字符数组中
1004+
*
1005+
* @param file 文件
1006+
* @return 字符数组
1007+
*/
1008+
public static byte[] readFile2BytesByChannel(File file) {
1009+
if (file == null) return null;
1010+
FileChannel channel = null;
1011+
FileInputStream fis = null;
1012+
try {
1013+
fis = new FileInputStream(file);
1014+
channel = fis.getChannel();
1015+
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
1016+
while (true) {
1017+
if (!((channel.read(byteBuffer)) > 0)) break;
1018+
}
1019+
return byteBuffer.array();
1020+
} catch (IOException e) {
1021+
e.printStackTrace();
1022+
return null;
1023+
} finally {
1024+
CloseUtils.closeIO(channel, fis);
1025+
}
1026+
}
1027+
1028+
/**
1029+
* 读取文件到字符数组中
1030+
*
1031+
* @param filePath 文件路径
1032+
* @return 字符数组
1033+
*/
1034+
public static byte[] readFile2BytesByMap(String filePath) {
1035+
return readFile2BytesByMap(getFileByPath(filePath));
1036+
}
1037+
1038+
/**
1039+
* 读取文件到字符数组中
1040+
*
1041+
* @param file 文件
1042+
* @return 字符数组
1043+
*/
1044+
public static byte[] readFile2BytesByMap(File file) {
1045+
if (file == null) return null;
1046+
FileChannel fc = null;
1047+
try {
1048+
fc = new RandomAccessFile(file, "r").getChannel();
1049+
MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).load();
1050+
byte[] result = new byte[(int) fc.size()];
1051+
if (byteBuffer.remaining() > 0) {
1052+
byteBuffer.get(result, 0, byteBuffer.remaining());
1053+
}
1054+
return result;
1055+
} catch (IOException e) {
1056+
e.printStackTrace();
1057+
return null;
1058+
} finally {
1059+
CloseUtils.closeIO(fc);
1060+
}
1061+
}
1062+
9511063
/**
9521064
* 获取文件最后修改的毫秒时间戳
9531065
*

utilcode/src/main/java/com/blankj/utilcode/util/SPUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public SPUtils(String spName) {
3333
}
3434

3535
/**
36-
* SP中写入String类型value
36+
* SP中写入String
3737
*
3838
* @param key 键
3939
* @param value 值
@@ -64,7 +64,7 @@ public String getString(String key, String defaultValue) {
6464
}
6565

6666
/**
67-
* SP中写入int类型value
67+
* SP中写入int
6868
*
6969
* @param key 键
7070
* @param value 值
@@ -95,7 +95,7 @@ public int getInt(String key, int defaultValue) {
9595
}
9696

9797
/**
98-
* SP中写入long类型value
98+
* SP中写入long
9999
*
100100
* @param key 键
101101
* @param value 值
@@ -126,7 +126,7 @@ public long getLong(String key, long defaultValue) {
126126
}
127127

128128
/**
129-
* SP中写入float类型value
129+
* SP中写入float
130130
*
131131
* @param key 键
132132
* @param value 值
@@ -157,7 +157,7 @@ public float getFloat(String key, float defaultValue) {
157157
}
158158

159159
/**
160-
* SP中写入boolean类型value
160+
* SP中写入boolean
161161
*
162162
* @param key 键
163163
* @param value 值
@@ -188,7 +188,7 @@ public boolean getBoolean(String key, boolean defaultValue) {
188188
}
189189

190190
/**
191-
* SP中写入String集合类型value
191+
* SP中写入String集合
192192
*
193193
* @param key 键
194194
* @param values 值

0 commit comments

Comments
 (0)