Java Path Create nio createFileWithContents(String pathString, String contents)

Here you can find the source of createFileWithContents(String pathString, String contents)

Description

create File With Contents

License

Open Source License

Declaration

static public Path createFileWithContents(String pathString, String contents) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * SiniaUtils/*from w  ww.j  a  v  a2s  .  com*/
 * Copyright (c) 2011-2 Siniatech Ltd  
 * http://www.siniatech.com/products/siniautils
 *
 * All rights reserved. This project and the accompanying materials are made 
 * available under the terms of the MIT License which can be found in the root  
 * of the project, and at http://www.opensource.org/licenses/mit-license.php
 *
 ******************************************************************************/

import static java.nio.file.Files.*;

import java.io.BufferedWriter;
import java.io.IOException;

import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class Main {
    static public Path createFileWithContents(String pathString, String contents) throws IOException {
        Path path = FileSystems.getDefault().getPath(pathString);
        if (exists(path)) {
            throw new IOException("File " + pathString + " already exists.");
        } else {
            createFile(path);
        }
        try (BufferedWriter out = newBufferedWriter(path, Charset.defaultCharset())) {
            out.write(contents);
            return path;
        }
    }
}

Related

  1. createFileForReport(Path reportDir, String filePrefix, String fileSuffix)
  2. createFileFromText(String sourceText, IPath path)
  3. createFileIfNotExist(Path path)
  4. createFileIfNotExists(Path path)
  5. createFileRelativeToClasspath(String resourceName)
  6. createFolder(String workspacePath)
  7. createFoldersIfNecessary(String workspacePath)
  8. createFullURI(String val, Path parent)
  9. createGlobMatcher(final Path path)