11package com .blankj .utilcode .utils ;
22
33import java .io .BufferedInputStream ;
4+ import java .io .BufferedOutputStream ;
45import java .io .BufferedReader ;
56import java .io .Closeable ;
67import java .io .File ;
@@ -433,7 +434,7 @@ public static boolean writeFileFromIS(File file, InputStream is, boolean append)
433434 if (!createOrExistsFile (file )) return false ;
434435 OutputStream os = null ;
435436 try {
436- os = new FileOutputStream (file , append );
437+ os = new BufferedOutputStream ( new FileOutputStream (file , append ) );
437438 byte data [] = new byte [KB ];
438439 while (is .read (data ) != -1 ) os .write (data );
439440 return true ;
@@ -500,14 +501,14 @@ public static String getFileCharsetSimple(String filePath) {
500501 */
501502 public static String getFileCharsetSimple (File file ) {
502503 int p = 0 ;
503- BufferedInputStream bin = null ;
504+ InputStream is = null ;
504505 try {
505- bin = new BufferedInputStream (new FileInputStream (file ));
506- p = (bin .read () << 8 ) + bin .read ();
506+ is = new BufferedInputStream (new FileInputStream (file ));
507+ p = (is .read () << 8 ) + is .read ();
507508 } catch (IOException e ) {
508509 e .printStackTrace ();
509510 } finally {
510- closeIO (bin );
511+ closeIO (is );
511512 }
512513 switch (p ) {
513514 case 0xefbb :
@@ -558,63 +559,67 @@ public static int getFileLines(File file) {
558559 }
559560
560561 /**
561- * 按行读取文件
562+ * 指定编码按行读取文件到List
562563 *
563- * @param file 文件
564- * @return 行链表
564+ * @param filePath 文件路径
565+ * @param charsetName 编码格式
566+ * @return 文件行链表
565567 */
566- public static List <String > readFileByLine ( File file ) {
567- return readFileByLine ( file , null );
568+ public static List <String > readFile2List ( String filePath , String charsetName ) {
569+ return readFile2List ( getFileByPath ( filePath ), charsetName );
568570 }
569571
570572 /**
571- * 按行读取文件
573+ * 指定编码按行读取文件到List
572574 *
573575 * @param file 文件
574- * @param charsetName 字符编码格式
575- * @return 行链表
576+ * @param charsetName 编码格式
577+ * @return 文件行链表
576578 */
577- public static List <String > readFileByLine (File file , String charsetName ) {
578- if (file == null ) return null ;
579- List <String > list = new ArrayList <>();
580- BufferedReader reader = null ;
581- try {
582- if (charsetName == null ) {
583- reader = new BufferedReader (new FileReader (file ));
584- } else {
585- reader = new BufferedReader (new InputStreamReader (new FileInputStream (file ), charsetName ));
586- }
587- String line ;
588- while ((line = reader .readLine ()) != null ) {
589- list .add (line );
590- }
591- } catch (IOException e ) {
592- e .printStackTrace ();
593- } finally {
594- closeIO (reader );
595- }
596- return list ;
579+ public static List <String > readFile2List (File file , String charsetName ) {
580+ return readFile2List (file , 0 , 0x7FFFFFFF , charsetName );
597581 }
598582
599583 /**
600- * 读取前几行数据
584+ * 指定编码按行读取文件到List
601585 *
602- * @param file 文件
603- * @param endLineNum 需要读取的行数
586+ * @param filePath 文件路径
587+ * @param start 需要读取的开始行数
588+ * @param end 需要读取的结束行数
589+ * @param charsetName 编码格式
590+ * @return 包含制定行的list
591+ */
592+ public static List <String > readFile2List (String filePath , int start , int end , String charsetName ) {
593+ return readFile2List (getFileByPath (filePath ), start , end , charsetName );
594+ }
595+
596+ /**
597+ * 指定编码按行读取文件到List
598+ *
599+ * @param file 文件
600+ * @param start 需要读取的开始行数
601+ * @param end 需要读取的结束行数
602+ * @param charsetName 编码格式
604603 * @return 包含制定行的list
605604 */
606- public static List <String > readFileByLine (File file , int endLineNum ) {
605+ public static List <String > readFile2List (File file , int start , int end , String charsetName ) {
607606 if (file == null ) return null ;
608- List <String > list = new ArrayList <>();
607+ if (start > end ) return null ;
608+ List <String > list = null ;
609609 BufferedReader reader = null ;
610610 try {
611- reader = new BufferedReader (new FileReader (file ));
612611 String line ;
612+ int curLine = 1 ;
613+ list = new ArrayList <>();
614+ if (StringUtils .isSpace (charsetName )) {
615+ reader = new BufferedReader (new FileReader (file ));
616+ } else {
617+ reader = new BufferedReader (new InputStreamReader (new FileInputStream (file ), charsetName ));
618+ }
613619 while ((line = reader .readLine ()) != null ) {
614- list .add (line );
615- if (list .size () == endLineNum ) {
616- break ;
617- }
620+ if (curLine > end ) break ;
621+ if (start <= curLine && curLine <= end ) list .add (line );
622+ ++curLine ;
618623 }
619624 } catch (IOException e ) {
620625 e .printStackTrace ();
@@ -624,73 +629,44 @@ public static List<String> readFileByLine(File file, int endLineNum) {
624629 return list ;
625630 }
626631
627- public static StringBuilder readFile (String filePath , String charsetName ) {
628- File file = new File (filePath );
629- if (!file .isFile ()) return null ;
630- return readFile (file , charsetName );
632+ /**
633+ * 指定编码按行读取文件到StringBuilder中
634+ *
635+ * @param filePath 文件路径
636+ * @param charsetName 编码格式
637+ * @return StringBuilder对象
638+ */
639+ public static StringBuilder readFile2SB (String filePath , String charsetName ) {
640+ return readFile2SB (getFileByPath (filePath ), charsetName );
631641 }
632642
633- public static StringBuilder readFile (File file , String charsetName ) {
634- StringBuilder sb = new StringBuilder ("" );
643+ /**
644+ * 指定编码按行读取文件到StringBuilder中
645+ *
646+ * @param file 文件
647+ * @param charsetName 编码格式
648+ * @return StringBuilder对象
649+ */
650+ public static StringBuilder readFile2SB (File file , String charsetName ) {
651+ if (file == null ) return null ;
652+ StringBuilder sb = null ;
635653 BufferedReader reader = null ;
636654 try {
637- InputStreamReader is = new InputStreamReader ( new FileInputStream ( file ), charsetName );
638- reader = new BufferedReader (is );
655+ sb = new StringBuilder ( );
656+ reader = new BufferedReader (new InputStreamReader ( new FileInputStream ( file ), charsetName ) );
639657 String line ;
640658 while ((line = reader .readLine ()) != null ) {
641- if (!sb .toString ().equals ("" )) {
642- sb .append ("\r \n " );
643- }
644659 sb .append (line );
660+ sb .append ("\r \n " );// windows系统换行为\r\n,Linux为\n
645661 }
646- reader .close ();
647662 } catch (IOException e ) {
648663 e .printStackTrace ();
649664 } finally {
650- if (reader != null ) {
651- try {
652- reader .close ();
653- } catch (IOException e ) {
654- e .printStackTrace ();
655- }
656- }
665+ closeIO (reader );
657666 }
658667 return sb ;
659668 }
660669
661-
662- /**
663- * @param filePath
664- * @param charsetName
665- * @return
666- */
667- public static List <String > readFileToList (String filePath , String charsetName ) {
668- File file = new File (filePath );
669- if (!file .isFile ()) return null ;
670- List <String > fileContent = new ArrayList <>();
671- BufferedReader reader = null ;
672- try {
673- InputStreamReader is = new InputStreamReader (new FileInputStream (file ), charsetName );
674- reader = new BufferedReader (is );
675- String line ;
676- while ((line = reader .readLine ()) != null ) {
677- fileContent .add (line );
678- }
679- reader .close ();
680- return fileContent ;
681- } catch (IOException e ) {
682- throw new RuntimeException ("IOException occurred. " , e );
683- } finally {
684- if (reader != null ) {
685- try {
686- reader .close ();
687- } catch (IOException e ) {
688- e .printStackTrace ();
689- }
690- }
691- }
692- }
693-
694670 /**
695671 * byte单位转换(单位:unit)
696672 *
@@ -750,23 +726,35 @@ public static double getFileSize(File file, int unit) {
750726 }
751727
752728 /**
753- * 获取文件路径的父级目录
729+ * 根据全路径获取最长目录
754730 *
755- * @param filePath
756- * @return
731+ * @param filePath 文件路径
732+ * @return filePath最长目录
757733 */
758734 public static String getDirName (String filePath ) {
759735 if (StringUtils .isSpace (filePath )) return filePath ;
760736 int lastSep = filePath .lastIndexOf (File .separator );
761737 return lastSep == -1 ? "" : filePath .substring (0 , lastSep + 1 );
762738 }
763739
740+ /**
741+ * 根据全路径获取文件名
742+ *
743+ * @param filePath 文件路径
744+ * @return 文件名
745+ */
764746 public static String getFileName (String filePath ) {
765747 if (StringUtils .isSpace (filePath )) return filePath ;
766748 int lastSep = filePath .lastIndexOf (File .separator );
767749 return lastSep == -1 ? filePath : filePath .substring (lastSep + 1 );
768750 }
769751
752+ /**
753+ * 根据全路径获取文件名不带拓展名
754+ *
755+ * @param filePath 文件路径
756+ * @return 文件名不带拓展名
757+ */
770758 public static String getFileNameNoExtension (String filePath ) {
771759 if (StringUtils .isSpace (filePath )) return filePath ;
772760 int lastPoi = filePath .lastIndexOf ('.' );
@@ -780,6 +768,12 @@ public static String getFileNameNoExtension(String filePath) {
780768 return filePath .substring (lastSep + 1 , lastPoi );
781769 }
782770
771+ /**
772+ * 根据全路径获取文件拓展名
773+ *
774+ * @param filePath 文件路径
775+ * @return 文件拓展名
776+ */
783777 public static String getFileExtension (String filePath ) {
784778 if (StringUtils .isSpace (filePath )) return filePath ;
785779 int lastPoi = filePath .lastIndexOf ('.' );
0 commit comments