Skip to content

Commit

Permalink
Update file utils
Browse files Browse the repository at this point in the history
Add support to append next line character after each line while
reading string from a file.
  • Loading branch information
pranavpandey committed Jun 18, 2023
1 parent 5da800b commit e4ef241
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public class DynamicFileUtils {
*/
public static final String EXTENSION_IMAGE = ".png";

/**
* Constant for the next line character.
*/
public static final String CHARACTER_NEXT_LINE = "\n";

/**
* Returns the default external directory for the app appended with the supplied path.
*
Expand Down Expand Up @@ -642,11 +647,14 @@ public static boolean writeStringToFile(@NonNull Context context,
*
* @param context The context to get content resolver.
* @param fileUri The source file URI.
* @param nextLine {@code true} to append next line character after each line.
*
* @return The string data after reading the file.
*
* @see #CHARACTER_NEXT_LINE
*/
public static @Nullable String readStringFromFile(
@Nullable Context context, @Nullable Uri fileUri) {
public static @Nullable String readStringFromFile(@Nullable Context context,
@Nullable Uri fileUri, boolean nextLine) {
if (context == null || fileUri == null) {
return null;
}
Expand All @@ -664,6 +672,10 @@ public static boolean writeStringToFile(@NonNull Context context,
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);

if (nextLine) {
stringBuilder.append(CHARACTER_NEXT_LINE);
}
}
} catch (Exception ignored) {
} finally {
Expand All @@ -679,6 +691,21 @@ public static boolean writeStringToFile(@NonNull Context context,
return string;
}

/**
* Reads a string data from the file URI.
*
* @param context The context to get content resolver.
* @param fileUri The source file URI.
*
* @return The string data after reading the file.
*
* @see #readStringFromFile(Context, Uri, boolean)
*/
public static @Nullable String readStringFromFile(
@Nullable Context context, @Nullable Uri fileUri) {
return readStringFromFile(context, fileUri, false);
}

/**
* Save and returns URI from the bitmap.
* <p>It will automatically use the @link FileProvider} on API 24 and above.
Expand Down

0 comments on commit e4ef241

Please sign in to comment.