|
1 | 1 | package org.unify4j.common;
|
2 | 2 |
|
3 |
| -import java.io.FileOutputStream; |
4 |
| -import java.io.IOException; |
5 |
| -import java.io.RandomAccessFile; |
6 |
| -import java.io.Serializable; |
| 3 | +import java.io.*; |
7 | 4 | import java.nio.ByteBuffer;
|
8 | 5 | import java.nio.channels.AsynchronousFileChannel;
|
9 | 6 | import java.nio.channels.CompletionHandler;
|
@@ -670,6 +667,39 @@ public static void readFileEachLinesIfNeeded(String filename, ReaderAsyncCallbac
|
670 | 667 | readFileEachLinesIfNeeded(toPath(filename), callback);
|
671 | 668 | }
|
672 | 669 |
|
| 670 | + /** |
| 671 | + * Reads the content of a specific file and keeps the format content of the file. |
| 672 | + * |
| 673 | + * @param filename The path to the file to read. |
| 674 | + * @return The content of the file as a string. |
| 675 | + * @throws IOException If an I/O error occurs. |
| 676 | + */ |
| 677 | + public static String readFileKeepFormat(Path filename) throws IOException { |
| 678 | + if (!exists(filename)) { |
| 679 | + return ""; |
| 680 | + } |
| 681 | + StringBuilder builder = new StringBuilder(); |
| 682 | + try (BufferedReader reader = Files.newBufferedReader(filename)) { |
| 683 | + char[] buffer = new char[8192]; // Read in chunks of 8KB |
| 684 | + int i; |
| 685 | + while ((i = reader.read(buffer)) != -1) { |
| 686 | + builder.append(buffer, 0, i); |
| 687 | + } |
| 688 | + } |
| 689 | + return builder.toString(); |
| 690 | + } |
| 691 | + |
| 692 | + /** |
| 693 | + * Reads the content of a specific file and keeps the format content of the file. |
| 694 | + * |
| 695 | + * @param filename The path to the file to read. |
| 696 | + * @return The content of the file as a string. |
| 697 | + * @throws IOException If an I/O error occurs. |
| 698 | + */ |
| 699 | + public static String readFileKeepFormat(String filename) throws IOException { |
| 700 | + return readFileKeepFormat(toPath(filename)); |
| 701 | + } |
| 702 | + |
673 | 703 | /**
|
674 | 704 | * Traverses the file tree rooted at the given path and invokes a callback for each file and directory encountered.
|
675 | 705 | *
|
|
0 commit comments