Java Path File Content saveFile(String workspacePath, byte[] content)

Here you can find the source of saveFile(String workspacePath, byte[] content)

Description

save File

License

Apache License

Declaration

public static void saveFile(String workspacePath, byte[] content) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void saveFile(String workspacePath, byte[] content) throws FileNotFoundException, IOException {
        createFoldersIfNecessary(workspacePath);
        Path path = FileSystems.getDefault().getPath(workspacePath);
        Files.write(path, content);
    }/*from w  w w .  ja  v a2s  . c  o m*/

    public static void createFoldersIfNecessary(String workspacePath) {
        int lastIndexOf = workspacePath.lastIndexOf(File.separator);
        if (lastIndexOf > 0) {
            String directory = workspacePath.substring(0, lastIndexOf);
            createFolder(directory);
        }
    }

    public static boolean createFolder(String workspacePath) {
        File folder = new File(workspacePath);
        if (!folder.exists()) {
            return folder.mkdirs();
        }
        return true;
    }

    public static boolean exists(String repositoryName) {
        if (repositoryName == null || "".equals(repositoryName)) {
            return false;
        }
        Path path;
        try {
            path = FileSystems.getDefault().getPath(repositoryName);
        } catch (java.nio.file.InvalidPathException e) {
            return false;
        }
        return Files.exists(path);
    }
}

Related

  1. getFileContents(Path file)
  2. getFileContents(String path)
  3. listUris(Path content)
  4. readContent(Path file)
  5. saveByteArrayToFile(byte[] content, Path targetPath)
  6. slurpFileContent(Path path)
  7. write2File(String contents, Path outputPath)
  8. WriteContentsToFile(String planeTextFilePath, byte[] encryptedData)
  9. writeFile(final String content, final String path)