Java Utililty Methods Path File Content

List of utility methods to do Path File Content

Description

The list of methods to do Path File Content are organized into topic(s).

Method

StringreadContent(Path file)
read Content
List<String> lines = Files.readAllLines(file, DEFAULT_CHARSET);
StringBuilder sb = new StringBuilder();
for (String line : lines) {
    sb.append(line).append("\n");
return sb.toString();
voidsaveByteArrayToFile(byte[] content, Path targetPath)
save Byte Array To File
File file = targetPath.toFile();
if (!file.exists()) {
    file.createNewFile();
FileOutputStream fop = new FileOutputStream(file);
InputStream is = new ByteArrayInputStream(content);
byte[] buf = new byte[4096];
int bytesRead;
...
voidsaveFile(String workspacePath, byte[] content)
save File
createFoldersIfNecessary(workspacePath);
Path path = FileSystems.getDefault().getPath(workspacePath);
Files.write(path, content);
StringslurpFileContent(Path path)
Read the content for the given path
byte[] buf = new byte[(int) Files.size(path)];
try (InputStream in = Files.newInputStream(path)) {
    in.read(buf);
    return new String(buf);
voidwrite2File(String contents, Path outputPath)
write File
if (outputPath != null) {
    try {
        outputPath.toFile().getParentFile().mkdirs();
        Files.write(outputPath, contents.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
voidWriteContentsToFile(String planeTextFilePath, byte[] encryptedData)
Write Contents To File
Files.write(Paths.get(planeTextFilePath), encryptedData);
voidwriteFile(final String content, final String path)
write File
try {
    final PrintWriter writer = new PrintWriter(path);
    writer.print("");
    writer.close();
    Files.write(Paths.get(path), content.getBytes(), StandardOpenOption.CREATE);
} catch (final IOException e) {
    e.printStackTrace();
voidwriteFile(Path file, String content)
write File
try {
    Files.write(file, content.getBytes(StandardCharsets.UTF_8));
} catch (final IOException e) {
    throw new AssertionError("Error writing to file " + file);
voidwriteFile(Path filePath, String content)
Writes a file with content to specified filePath .
PrintWriter writer = null;
try {
    writer = new PrintWriter(filePath.toString(), "UTF-8");
    writer.print(content);
} finally {
    if (writer != null) {
        writer.close();
voidwriteFile(Path path, String content)
write File
PrintWriter writer = null;
try {
    writer = new PrintWriter(path.toString(), "UTF-8");
    writer.print(content);
} finally {
    if (writer != null) {
        writer.close();