Java Path Create nio createFileFromText(String sourceText, IPath path)

Here you can find the source of createFileFromText(String sourceText, IPath path)

Description

Creates a file from input string text.

License

Open Source License

Parameter

Parameter Description
sourceText the file's contents
path the absolute path of the file to create

Exception

Parameter Description
IOException if creating the file fails

Declaration

public static void createFileFromText(String sourceText, IPath path) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./* ww w .j av  a2s . c  o  m*/
 *******************************************************************************/

import com.google.common.base.Preconditions;
import org.eclipse.core.runtime.IPath;
import java.io.IOException;
import java.nio.file.FileSystems;

import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    /**
     * Creates a file from input string text.
     *
     * @param sourceText the file's contents
     * @param path the absolute path of the file to create
     * @throws IOException if creating the file fails
     */
    public static void createFileFromText(String sourceText, IPath path) throws IOException {
        Preconditions.checkArgument(path.isAbsolute());
        Path file = FileSystems.getDefault().getPath(path.toOSString());
        Files.write(file, sourceText.getBytes());
    }
}

Related

  1. createFile(Path path)
  2. createFile(Path path)
  3. createFile(String filePath, String content)
  4. createFile(String workspacePath)
  5. createFileForReport(Path reportDir, String filePrefix, String fileSuffix)
  6. createFileIfNotExist(Path path)
  7. createFileIfNotExists(Path path)
  8. createFileRelativeToClasspath(String resourceName)
  9. createFileWithContents(String pathString, String contents)