|
1 | 1 | package com.blankj.utilcode.utils; |
2 | 2 |
|
3 | 3 | import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
4 | 5 | import java.io.BufferedReader; |
5 | 6 | import java.io.Closeable; |
6 | 7 | import java.io.File; |
@@ -433,7 +434,7 @@ public static boolean writeFileFromIS(File file, InputStream is, boolean append) |
433 | 434 | if (!createOrExistsFile(file)) return false; |
434 | 435 | OutputStream os = null; |
435 | 436 | try { |
436 | | - os = new FileOutputStream(file, append); |
| 437 | + os = new BufferedOutputStream(new FileOutputStream(file, append)); |
437 | 438 | byte data[] = new byte[KB]; |
438 | 439 | while (is.read(data) != -1) os.write(data); |
439 | 440 | return true; |
@@ -500,14 +501,14 @@ public static String getFileCharsetSimple(String filePath) { |
500 | 501 | */ |
501 | 502 | public static String getFileCharsetSimple(File file) { |
502 | 503 | int p = 0; |
503 | | - BufferedInputStream bin = null; |
| 504 | + InputStream is = null; |
504 | 505 | 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(); |
507 | 508 | } catch (IOException e) { |
508 | 509 | e.printStackTrace(); |
509 | 510 | } finally { |
510 | | - closeIO(bin); |
| 511 | + closeIO(is); |
511 | 512 | } |
512 | 513 | switch (p) { |
513 | 514 | case 0xefbb: |
@@ -558,63 +559,67 @@ public static int getFileLines(File file) { |
558 | 559 | } |
559 | 560 |
|
560 | 561 | /** |
561 | | - * 按行读取文件 |
| 562 | + * 指定编码按行读取文件到List |
562 | 563 | * |
563 | | - * @param file 文件 |
564 | | - * @return 行链表 |
| 564 | + * @param filePath 文件路径 |
| 565 | + * @param charsetName 编码格式 |
| 566 | + * @return 文件行链表 |
565 | 567 | */ |
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); |
568 | 570 | } |
569 | 571 |
|
570 | 572 | /** |
571 | | - * 按行读取文件 |
| 573 | + * 指定编码按行读取文件到List |
572 | 574 | * |
573 | 575 | * @param file 文件 |
574 | | - * @param charsetName 字符编码格式 |
575 | | - * @return 行链表 |
| 576 | + * @param charsetName 编码格式 |
| 577 | + * @return 文件行链表 |
576 | 578 | */ |
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); |
597 | 581 | } |
598 | 582 |
|
599 | 583 | /** |
600 | | - * 读取前几行数据 |
| 584 | + * 指定编码按行读取文件到List |
601 | 585 | * |
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 编码格式 |
604 | 603 | * @return 包含制定行的list |
605 | 604 | */ |
606 | | - public static List<String> readFileByLine(File file, int endLineNum) { |
| 605 | + public static List<String> readFile2List(File file, int start, int end, String charsetName) { |
607 | 606 | if (file == null) return null; |
608 | | - List<String> list = new ArrayList<>(); |
| 607 | + if (start > end) return null; |
| 608 | + List<String> list = null; |
609 | 609 | BufferedReader reader = null; |
610 | 610 | try { |
611 | | - reader = new BufferedReader(new FileReader(file)); |
612 | 611 | 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 | + } |
613 | 619 | 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; |
618 | 623 | } |
619 | 624 | } catch (IOException e) { |
620 | 625 | e.printStackTrace(); |
@@ -624,73 +629,44 @@ public static List<String> readFileByLine(File file, int endLineNum) { |
624 | 629 | return list; |
625 | 630 | } |
626 | 631 |
|
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); |
631 | 641 | } |
632 | 642 |
|
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; |
635 | 653 | BufferedReader reader = null; |
636 | 654 | 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)); |
639 | 657 | String line; |
640 | 658 | while ((line = reader.readLine()) != null) { |
641 | | - if (!sb.toString().equals("")) { |
642 | | - sb.append("\r\n"); |
643 | | - } |
644 | 659 | sb.append(line); |
| 660 | + sb.append("\r\n");// windows系统换行为\r\n,Linux为\n |
645 | 661 | } |
646 | | - reader.close(); |
647 | 662 | } catch (IOException e) { |
648 | 663 | e.printStackTrace(); |
649 | 664 | } finally { |
650 | | - if (reader != null) { |
651 | | - try { |
652 | | - reader.close(); |
653 | | - } catch (IOException e) { |
654 | | - e.printStackTrace(); |
655 | | - } |
656 | | - } |
| 665 | + closeIO(reader); |
657 | 666 | } |
658 | 667 | return sb; |
659 | 668 | } |
660 | 669 |
|
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 | | - |
694 | 670 | /** |
695 | 671 | * byte单位转换(单位:unit) |
696 | 672 | * |
@@ -750,23 +726,35 @@ public static double getFileSize(File file, int unit) { |
750 | 726 | } |
751 | 727 |
|
752 | 728 | /** |
753 | | - * 获取文件路径的父级目录 |
| 729 | + * 根据全路径获取最长目录 |
754 | 730 | * |
755 | | - * @param filePath |
756 | | - * @return |
| 731 | + * @param filePath 文件路径 |
| 732 | + * @return filePath最长目录 |
757 | 733 | */ |
758 | 734 | public static String getDirName(String filePath) { |
759 | 735 | if (StringUtils.isSpace(filePath)) return filePath; |
760 | 736 | int lastSep = filePath.lastIndexOf(File.separator); |
761 | 737 | return lastSep == -1 ? "" : filePath.substring(0, lastSep + 1); |
762 | 738 | } |
763 | 739 |
|
| 740 | + /** |
| 741 | + * 根据全路径获取文件名 |
| 742 | + * |
| 743 | + * @param filePath 文件路径 |
| 744 | + * @return 文件名 |
| 745 | + */ |
764 | 746 | public static String getFileName(String filePath) { |
765 | 747 | if (StringUtils.isSpace(filePath)) return filePath; |
766 | 748 | int lastSep = filePath.lastIndexOf(File.separator); |
767 | 749 | return lastSep == -1 ? filePath : filePath.substring(lastSep + 1); |
768 | 750 | } |
769 | 751 |
|
| 752 | + /** |
| 753 | + * 根据全路径获取文件名不带拓展名 |
| 754 | + * |
| 755 | + * @param filePath 文件路径 |
| 756 | + * @return 文件名不带拓展名 |
| 757 | + */ |
770 | 758 | public static String getFileNameNoExtension(String filePath) { |
771 | 759 | if (StringUtils.isSpace(filePath)) return filePath; |
772 | 760 | int lastPoi = filePath.lastIndexOf('.'); |
@@ -780,6 +768,12 @@ public static String getFileNameNoExtension(String filePath) { |
780 | 768 | return filePath.substring(lastSep + 1, lastPoi); |
781 | 769 | } |
782 | 770 |
|
| 771 | + /** |
| 772 | + * 根据全路径获取文件拓展名 |
| 773 | + * |
| 774 | + * @param filePath 文件路径 |
| 775 | + * @return 文件拓展名 |
| 776 | + */ |
783 | 777 | public static String getFileExtension(String filePath) { |
784 | 778 | if (StringUtils.isSpace(filePath)) return filePath; |
785 | 779 | int lastPoi = filePath.lastIndexOf('.'); |
|