Example usage for javax.tools FileObject openWriter

List of usage examples for javax.tools FileObject openWriter

Introduction

In this page you can find the example usage for javax.tools FileObject openWriter.

Prototype

Writer openWriter() throws IOException;

Source Link

Document

Returns a Writer for this file object.

Usage

From source file:uniol.apt.compiler.AbstractServiceProcessor.java

/**
 * Function that writes a resource list into the class output directory.
 * Existing entries are preserved. This means that this function only ever adds new entries.
 * @param resourceName Name of the file in which the resource list should be saved.
 * @param entries Entries that should be added to the list.
 * @throws IOException In case I/O errors occur.
 *//*from  w  w w .j  ava2 s  .  c  om*/
protected void writeResourceList(String resourceName, Collection<String> entries) throws IOException {
    entries = new TreeSet<>(entries);

    // read already listed services
    try {
        FileObject fo = this.filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
        try (InputStream is = fo.openInputStream()) {
            LineIterator lIter = IOUtils.lineIterator(is, "UTF-8");
            while (lIter.hasNext()) {
                String entry = lIter.next();
                entries.add(entry);
            }
        }
    } catch (IOException ex) {
        /* It's ok if the resource can't get found; we only skip reading it */
    }

    // write new list
    FileObject fo = this.filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
    Writer writer = fo.openWriter();
    for (String entry : entries) {
        writer.append(entry + "\n");
    }
    writer.close();
}