Skip to content

Commit 523aa37

Browse files
committed
✨ feat: add unify functions for Os4j #4
1 parent b1c7ee3 commit 523aa37

File tree

1 file changed

+34
-4
lines changed
  • plugin/src/main/groovy/org/unify4j/common

1 file changed

+34
-4
lines changed

plugin/src/main/groovy/org/unify4j/common/Os4j.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.unify4j.common;
22

3-
import java.io.FileOutputStream;
4-
import java.io.IOException;
5-
import java.io.RandomAccessFile;
6-
import java.io.Serializable;
3+
import java.io.*;
74
import java.nio.ByteBuffer;
85
import java.nio.channels.AsynchronousFileChannel;
96
import java.nio.channels.CompletionHandler;
@@ -670,6 +667,39 @@ public static void readFileEachLinesIfNeeded(String filename, ReaderAsyncCallbac
670667
readFileEachLinesIfNeeded(toPath(filename), callback);
671668
}
672669

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+
673703
/**
674704
* Traverses the file tree rooted at the given path and invokes a callback for each file and directory encountered.
675705
*

0 commit comments

Comments
 (0)