Java Path File Name nio saveToFile(String content, String directoryPath, String filename)

Here you can find the source of saveToFile(String content, String directoryPath, String filename)

Description

save To File

License

Open Source License

Declaration

public static void saveToFile(String content, String directoryPath, String filename) 

Method Source Code


//package com.java2s;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static void saveToFile(String content, String directoryPath, String filename) {
        createDirIfDoesNotExist(directoryPath);
        try {/*from  w ww .  j  a  va 2  s .  c o  m*/
            Files.write(Paths.get(directoryPath + filename), content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void createDirIfDoesNotExist(String websitesDirPath) {
        Path directoryPath = Paths.get(websitesDirPath);

        if (!Files.exists(directoryPath)) {
            try {
                Files.createDirectory(directoryPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. rename(Path origin, Path destination)
  2. renameFile(Path filePath, String postfix)
  3. renameLogFile(Path original)
  4. saveFileFromByteArray(String filename, String pathDirectory, byte[] data)
  5. saveNamesToFile(Map namesMap, Path namesFile)
  6. shortenFileName(Path file, List dirs)
  7. storeFile(String fileName, InputStream inputStream, Path targetPath)
  8. subDirectoryNames(final Path dir)
  9. toClassName(String path)