1313import java .io .FileInputStream ;
1414import java .io .FileNotFoundException ;
1515import java .io .FileOutputStream ;
16- import java .io .FileReader ;
1716import java .io .FileWriter ;
1817import java .io .FilenameFilter ;
1918import java .io .IOException ;
2019import java .io .InputStream ;
2120import java .io .InputStreamReader ;
2221import java .io .OutputStream ;
22+ import java .io .RandomAccessFile ;
23+ import java .nio .ByteBuffer ;
24+ import java .nio .MappedByteBuffer ;
25+ import java .nio .channels .FileChannel ;
2326import java .security .DigestInputStream ;
2427import java .security .MessageDigest ;
2528import java .security .NoSuchAlgorithmException ;
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 *
0 commit comments