Java Path File Write nio write(Path p, String s)

Here you can find the source of write(Path p, String s)

Description

Writes the specified string to a file.

License

Open Source License

Parameter

Parameter Description
p file destination (existing files will be overwritten)
s string to save

Exception

Parameter Description
IOException if writing to file fails

Declaration

public static void write(Path p, String s) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Main {
    /**/*from w w w  . j a v a 2 s .  c  o m*/
     * Writes the specified string to a file.
     *
     * @param p
     *          file destination (existing files will be overwritten)
     * @param s
     *          string to save
     * 
     * @throws IOException
     *           if writing to file fails
     */
    public static void write(Path p, String s) throws IOException {
        try (BufferedWriter writer = Files.newBufferedWriter(p, Charset.forName("UTF-8"), StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING)) {
            writer.write(s, 0, s.length());
        }
    }
}

Related

  1. setPosixPermissions(String path, boolean ownerRead, boolean ownerWrite, boolean ownerExecute, boolean groupRead, boolean groupWrite, boolean groupExecute, boolean othersRead, boolean othersWrite, boolean othersExecute)
  2. srcImageToSavePath(String src, Path saveFolder)
  3. write(List list, String outputPath)
  4. write(Object obj, String filePath)
  5. write(Path p)
  6. write(Path path, String string)
  7. writeAudio(String source, String writePath)
  8. writeEndStringInFile(Path path, String s)
  9. writeFile(String filePath, String text)