Changed!: Rename FileUtils readStringFromFile() to readTextFromFile() and writeStringToFile() to writeTextToFile()

This commit is contained in:
agnostic-apollo
2021-10-25 18:37:40 +05:00
parent 7bbc12c7c9
commit 37b9bcf5af
7 changed files with 27 additions and 27 deletions

View File

@@ -1375,22 +1375,22 @@ public class FileUtils {
/**
* Read a {@link String} from file at path with a specific {@link Charset} into {@code dataString}.
* Read a text {@link String} from file at path with a specific {@link Charset} into {@code dataString}.
*
* @param label The optional label for file to read. This can optionally be {@code null}.
* @param filePath The {@code path} for file to read.
* @param charset The {@link Charset} of the file. If this is {@code null},
* * then default {@link Charset} will be used.
* then default {@link Charset} will be used.
* @param dataStringBuilder The {@code StringBuilder} to read data into.
* @param ignoreNonExistentFile The {@code boolean} that decides if it should be considered an
* error if file to read doesn't exist.
* @return Returns the {@code error} if reading was not successful, otherwise {@code null}.
*/
public static Error readStringFromFile(String label, final String filePath, Charset charset, @NonNull final StringBuilder dataStringBuilder, final boolean ignoreNonExistentFile) {
public static Error readTextFromFile(String label, final String filePath, Charset charset, @NonNull final StringBuilder dataStringBuilder, final boolean ignoreNonExistentFile) {
label = (label == null ? "" : label + " ");
if (filePath == null || filePath.isEmpty()) return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError(label + "file path", "readStringFromFile");
Logger.logVerbose(LOG_TAG, "Reading string from " + label + "file at path \"" + filePath + "\"");
Logger.logVerbose(LOG_TAG, "Reading text from " + label + "file at path \"" + filePath + "\"");
Error error;
@@ -1423,7 +1423,7 @@ public class FileUtils {
FileInputStream fileInputStream = null;
BufferedReader bufferedReader = null;
try {
// Read string from file
// Read text from file
fileInputStream = new FileInputStream(filePath);
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, charset));
@@ -1437,7 +1437,7 @@ public class FileUtils {
Logger.logVerbose(LOG_TAG, Logger.getMultiLineLogStringEntry("String", DataUtils.getTruncatedCommandOutput(dataStringBuilder.toString(), Logger.LOGGER_ENTRY_MAX_SAFE_PAYLOAD, true, false, true), "-"));
} catch (Exception e) {
return FileUtilsErrno.ERRNO_READING_STRING_FROM_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
return FileUtilsErrno.ERRNO_READING_TEXT_FROM_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
} finally {
closeCloseable(fileInputStream);
closeCloseable(bufferedReader);
@@ -1497,7 +1497,7 @@ public class FileUtils {
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try {
// Read string from file
// Read serializable object from file
fileInputStream = new FileInputStream(filePath);
objectInputStream = new ObjectInputStream(fileInputStream);
//serializableObject = (T) objectInputStream.readObject();
@@ -1515,7 +1515,7 @@ public class FileUtils {
}
/**
* Write the {@link String} {@code dataString} with a specific {@link Charset} to file at path.
* Write text {@code dataString} with a specific {@link Charset} to file at path.
*
* @param label The optional label for file to write. This can optionally be {@code null}.
* @param filePath The {@code path} for file to write.
@@ -1525,11 +1525,11 @@ public class FileUtils {
* @param append The {@code boolean} that decides if file should be appended to or not.
* @return Returns the {@code error} if writing was not successful, otherwise {@code null}.
*/
public static Error writeStringToFile(String label, final String filePath, Charset charset, final String dataString, final boolean append) {
public static Error writeTextToFile(String label, final String filePath, Charset charset, final String dataString, final boolean append) {
label = (label == null ? "" : label + " ");
if (filePath == null || filePath.isEmpty()) return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError(label + "file path", "writeStringToFile");
Logger.logVerbose(LOG_TAG, Logger.getMultiLineLogStringEntry("Writing string to " + label + "file at path \"" + filePath + "\"", DataUtils.getTruncatedCommandOutput(dataString, Logger.LOGGER_ENTRY_MAX_SAFE_PAYLOAD, true, false, true), "-"));
Logger.logVerbose(LOG_TAG, Logger.getMultiLineLogStringEntry("Writing text to " + label + "file at path \"" + filePath + "\"", DataUtils.getTruncatedCommandOutput(dataString, Logger.LOGGER_ENTRY_MAX_SAFE_PAYLOAD, true, false, true), "-"));
Error error;
@@ -1547,14 +1547,14 @@ public class FileUtils {
FileOutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
// Write string to file
// Write text to file
fileOutputStream = new FileOutputStream(filePath, append);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, charset));
bufferedWriter.write(dataString);
bufferedWriter.flush();
} catch (Exception e) {
return FileUtilsErrno.ERRNO_WRITING_STRING_TO_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
return FileUtilsErrno.ERRNO_WRITING_TEXT_TO_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
} finally {
closeCloseable(fileOutputStream);
closeCloseable(bufferedWriter);
@@ -1586,7 +1586,7 @@ public class FileUtils {
FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
// Write object to file
// Write serializable object to file
fileOutputStream = new FileOutputStream(filePath);
objectOutputStream = new ObjectOutputStream(fileOutputStream);

View File

@@ -158,18 +158,18 @@ public class FileUtilsTests {
// Write "line1" to dir2/sub_reg1 regular file
label = dir2__sub_reg1_label; path = dir2__sub_reg1_path;
error = FileUtils.writeStringToFile(label, path, Charset.defaultCharset(), "line1", false);
error = FileUtils.writeTextToFile(label, path, Charset.defaultCharset(), "line1", false);
assertEqual("Failed to write string to " + label + " file with append mode false", null, error);
if (!FileUtils.regularFileExists(path, false))
throwException("The " + label + " file does not exist as expected after writing to it with append mode false");
// Write "line2" to dir2/sub_reg1 regular file
error = FileUtils.writeStringToFile(label, path, Charset.defaultCharset(), "\nline2", true);
error = FileUtils.writeTextToFile(label, path, Charset.defaultCharset(), "\nline2", true);
assertEqual("Failed to write string to " + label + " file with append mode true", null, error);
// Read dir2/sub_reg1 regular file
StringBuilder dataStringBuilder = new StringBuilder();
error = FileUtils.readStringFromFile(label, path, Charset.defaultCharset(), dataStringBuilder, false);
error = FileUtils.readTextFromFile(label, path, Charset.defaultCharset(), dataStringBuilder, false);
assertEqual("Failed to read from " + label + " file", null, error);
assertEqual("The data read from " + label + " file in not as expected", "line1\nline2", dataStringBuilder.toString());