Java File Write nio writeFile(String fileName, String content)

Here you can find the source of writeFile(String fileName, String content)

Description

write File

License

Open Source License

Declaration

public static void writeFile(String fileName, String content) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void writeFile(String fileName, String content) throws IOException {
        writeFile(fileName, new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
    }//from   w  ww. ja  va2 s.co  m

    public static void writeFile(String fileName, InputStream content) throws IOException {
        writeFile(new File(fileName), content);
    }

    public static void writeFile(File file, String content) throws IOException {
        writeFile(file, new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
    }

    public static void writeFile(File file, InputStream content) throws IOException {
        if (content == null) {
            throw new IllegalArgumentException("content must not be null");
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream stream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = content.read(buffer)) != -1) {
            stream.write(buffer, 0, length);
        }
        stream.close();
    }
}

Related

  1. writeFile(FileObject fileObject, String content)
  2. writeFile(List content, String targetFile)
  3. writeFile(OutputStream file, List data)
  4. writeFile(String filename, byte file[])
  5. writeFile(String fileName, String content)
  6. writeFile(String folder, String aFileName, String aItem)
  7. writeFile(String name, String content)