Here you can find the source of writeFile(FileObject fileObject, String content)
JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(fileName, originatingElements);
Parameter | Description |
---|---|
fileObject | a file object |
content | the file content |
Parameter | Description |
---|---|
IOException | when failed to write the file |
public static void writeFile(FileObject fileObject, String content) throws IOException
//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; } }