13
13
import java .io .FileInputStream ;
14
14
import java .io .FileNotFoundException ;
15
15
import java .io .FileOutputStream ;
16
- import java .io .FileReader ;
17
16
import java .io .FileWriter ;
18
17
import java .io .FilenameFilter ;
19
18
import java .io .IOException ;
20
19
import java .io .InputStream ;
21
20
import java .io .InputStreamReader ;
22
21
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 ;
23
26
import java .security .DigestInputStream ;
24
27
import java .security .MessageDigest ;
25
28
import java .security .NoSuchAlgorithmException ;
31
34
* <pre>
32
35
* author: Blankj
33
36
* blog : http://blankj.com
34
- * time : 2016/08/11
37
+ * time : 2016/05/03
35
38
* desc : 文件相关工具类
36
39
* </pre>
37
40
*/
@@ -41,6 +44,8 @@ private FileUtils() {
41
44
throw new UnsupportedOperationException ("u can't instantiate me..." );
42
45
}
43
46
47
+ private static final String LINE_SEP = System .getProperty ("line.separator" );
48
+
44
49
/**
45
50
* 根据文件路径获取文件
46
51
*
@@ -772,6 +777,42 @@ public static boolean writeFileFromIS(File file, InputStream is, boolean append)
772
777
}
773
778
}
774
779
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
+
775
816
/**
776
817
* 将字符串写入文件
777
818
*
@@ -862,7 +903,7 @@ public static List<String> readFile2List(File file, int st, int end, String char
862
903
int curLine = 1 ;
863
904
List <String > list = new ArrayList <>();
864
905
if (isSpace (charsetName )) {
865
- reader = new BufferedReader (new FileReader ( file ));
906
+ reader = new BufferedReader (new InputStreamReader ( new FileInputStream ( file ) ));
866
907
} else {
867
908
reader = new BufferedReader (new InputStreamReader (new FileInputStream (file ), charsetName ));
868
909
}
@@ -910,10 +951,10 @@ public static String readFile2String(File file, String charsetName) {
910
951
}
911
952
String line ;
912
953
while ((line = reader .readLine ()) != null ) {
913
- sb .append (line ).append (" \r \n " ); // windows系统换行为\r\n,Linux为\n
954
+ sb .append (line ).append (LINE_SEP );
914
955
}
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 ();
917
958
} catch (IOException e ) {
918
959
e .printStackTrace ();
919
960
return null ;
@@ -948,6 +989,77 @@ public static byte[] readFile2Bytes(File file) {
948
989
}
949
990
}
950
991
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
+
951
1063
/**
952
1064
* 获取文件最后修改的毫秒时间戳
953
1065
*
0 commit comments