Java File Write nio writeFile(FileObject fileObject, String content)

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

Description

Create a file using the javax.annotation.processing.Filer , look the example bellow JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(fileName, originatingElements);

License

Open Source License

Parameter

Parameter Description
fileObject a file object
content the file content

Exception

Parameter Description
IOException when failed to write the file

Declaration

public static void writeFile(FileObject fileObject, String content) throws IOException 

Method Source Code


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

import javax.tools.FileObject;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

public class Main {
    /**/*from w  ww .j  a v a 2 s  .  co  m*/
     * Create a file using the {@link javax.annotation.processing.Filer}, look the example bellow
     * <code>
     * JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(fileName, originatingElements);
     * </code>
     *
     * @param fileObject a file object
     * @param content    the file content
     * @throws IOException when failed to write the file
     */
    public static void writeFile(FileObject fileObject, String content) throws IOException {
        OutputStreamWriter osw = null;
        IOException exception = null;
        try {
            OutputStream os = fileObject.openOutputStream();
            osw = new OutputStreamWriter(os, Charset.forName("UTF-8"));
            osw.write(content, 0, content.length());
        } catch (IOException ex) {
            exception = ex;
        } finally {
            try {
                if (osw != null) {
                    osw.flush();
                    osw.close();
                }
            } catch (IOException ex) {
                exception = ex;
            }
        }
        if (exception != null)
            throw exception;
    }
}

Related

  1. writeFile(File file, String content)
  2. writeFile(File file, String content)
  3. writeFile(File file, String text)
  4. writeFile(List content, String targetFile)
  5. writeFile(OutputStream file, List data)
  6. writeFile(String filename, byte file[])
  7. writeFile(String fileName, String content)