Example usage for java.io FileWriter write

List of usage examples for java.io FileWriter write

Introduction

In this page you can find the example usage for java.io FileWriter write.

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:de.uniluebeck.itm.spyglass.core.ConfigStore.java

/**
 * Stores the configuration persistently into the given file.
 *
 * This method is synchronized to avoid storing into the same file concurrently.
 *
 * @param configFile/*  w  w w. ja va 2s.  c  o  m*/
 *            the file which will contain the configuration data afterwards
 */
protected synchronized void storeSync(final File configFile) {

    log.debug("Storing config to file: " + configFile);

    final StringWriter buf = new StringWriter();

    try {

        // If something happens here, the file will never have been opened.
        new Persister().write(spyglassConfig, buf);

        final FileWriter writer = new FileWriter(configFile);
        writer.write(buf.toString());
        writer.close();

    } catch (final ConcurrentModificationException e) {
        // Note: In theory this may lead to StackOverflow. But since under normal circumstances
        // the chances of hitting this Exception are already pretty small, we probably can risk
        // it.
        log.debug("Configuration was modified while the ConfigStore tried to store the config. Trying again...",
                e);
        storeSync(configFile);
        return;
    } catch (final IOException e) {
        log.error("Unable to store configuration output: Error while writing the file!", e);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (final Exception e) {
        log.error("Unable to store configuration output: " + e.getMessage(), e);
    }

    log.debug("Stored config to file: " + configFile + ".");

}

From source file:formatter.handler.get.FormatterGetHandler.java

private void dumpText(String fileName, String text) {
    try {// w ww .  jav a  2  s  .  c o m
        File textFile = new File(fileName);
        if (textFile.exists())
            textFile.delete();
        textFile.createNewFile();
        FileWriter fw = new FileWriter(textFile);
        fw.write(text);
        fw.close();
    } catch (Exception e) {
        // ignore
    }
}

From source file:com.stimulus.archiva.extraction.RTFExtractor.java

public Reader getText(InputStream is, Charset charset, IndexInfo indexInfo) throws ExtractionException {

    Reader reader = null;//from w w  w  .j  a v  a2  s  .  c o  m
    FileWriter writer = null;
    File file = null;
    try {
        reader = new InputStreamReader(is);
        file = File.createTempFile("extract_rtf", ".tmp");
        indexInfo.addDeleteFile(file);
        writer = new FileWriter(file);
        DefaultStyledDocument doc = new DefaultStyledDocument();
        new RTFEditorKit().read(reader, doc, 0);
        writer.write(doc.getText(0, doc.getLength()));
    } catch (Throwable ioe) {
        throw new ExtractionException("failed to parse rtf document", ioe, logger);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ioe) {
            }
        }

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ioe) {
            }
        }
    }
    try {
        Reader outReader = new FileReader(file);
        indexInfo.addReader(outReader);
        return outReader;
    } catch (Exception ex) {
        throw new ExtractionException("failed to extract text from powerpoint document", ex, logger,
                ChainedException.Level.DEBUG);
    }

}

From source file:test.gov.nih.nci.cacoresdk.domain.inheritance.implicit.TankResourceTest.java

/**
 * Uses Nested Search Criteria for search
 * Verifies that the results are returned 
 * Verifies size of the result set/*from  ww w . j a va2 s. co  m*/
 * Verifies that none of the attributes are null
 * 
 * @throws Exception
 */
public void testGet() throws Exception {

    try {

        Tank searchObject = new Tank();
        Collection results = getApplicationService()
                .search("gov.nih.nci.cacoresdk.domain.inheritance.implicit.Tank", searchObject);
        String id = "";

        if (results != null && results.size() > 0) {
            Tank obj = (Tank) ((List) results).get(0);

        } else
            return;

        if (id.equals(""))
            return;

        String url = baseURL + "/rest/Tank/" + id;

        WebClient client = WebClient.create(url);
        client.type("application/xml").accept("application/xml");
        Response response = client.get();

        if (response.getStatus() == Status.NOT_ACCEPTABLE.getStatusCode()) {
            InputStream is = (InputStream) response.getEntity();
            org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false);
            org.jdom.Document jDoc = builder.build(is);
            assertEquals(jDoc.getRootElement().getName(), "response");
        } else if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) {
            InputStream is = (InputStream) response.getEntity();
            org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false);
            org.jdom.Document jDoc = builder.build(is);
            assertEquals(jDoc.getRootElement().getName(), "response");
        } else if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }

        File myFile = new File("Tank" + "XML.xml");

        System.out.println("writing data to file " + myFile.getAbsolutePath());
        FileWriter myWriter = new FileWriter(myFile);

        BufferedReader br = new BufferedReader(new InputStreamReader(((InputStream) response.getEntity())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            myWriter.write(output);
            System.out.println(output);
        }

        myWriter.flush();
        myWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

}

From source file:ch.epfl.bbp.uima.annotationviewer.BlueAnnotationViewGenerator.java

/**
 * Automatically generates a style map file for the given analysis engine
 * metadata. The style map will be written to the file
 * <code>aStyleMapFile</code>.
 * //from ww w .j  a  v  a  2 s. c  om
 * 
 * @param aMetaData
 *            Metadata of the Analysis Engine whose outputs will be viewed
 *            using the generated style map.
 * @param aStyleMapFile
 *            file to which autogenerated style map will be written
 */
public void autoGenerateStyleMapFile(AnalysisEngineMetaData aMetaData, File aStyleMapFile) throws IOException {
    String xmlStr = autoGenerateStyleMap(aMetaData);
    FileWriter out = null;
    try {
        out = new FileWriter(aStyleMapFile);
        out.write(xmlStr);
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:ch.epfl.bbp.uima.annotationviewer.BlueAnnotationViewGenerator.java

/**
 * Automatically generates a style map file for the given type system. The
 * style map will be written to the file <code>aStyleMapFile</code>.
 * /*from ww  w .ja  v  a 2 s.  co m*/
 * @param aTypeSystem
 *            the type system for which a style map will be generated
 * @param aStyleMapFile
 *            file to which autogenerated style map will be written
 */
public void autoGenerateStyleMapFile(TypeSystemDescription aTypeSystem, File aStyleMapFile) throws IOException {
    String xmlStr = autoGenerateStyleMap(aTypeSystem);
    FileWriter out = null;
    try {
        out = new FileWriter(aStyleMapFile);
        out.write(xmlStr);
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:fi.hut.soberit.sensors.services.BatchDataUploadService.java

protected void writeFileHeader(FileWriter destination, List<Session> sessions) throws IOException {
    destination.write('{');

    if (sessions.size() > 0) {
        destination.write("\"sessions\": [");
    }/*  w w w .  j  a v a  2  s  .  c  o  m*/
}

From source file:edu.umn.msi.tropix.common.test.FileUtilsTest.java

@Test(groups = "unit")
public void getFileWriter() throws IOException {
    final FileWriter fileWriter = this.fileUtils.getFileWriter(this.file.getAbsolutePath());
    try {/*from ww  w.  jav  a2 s .  c  o  m*/
        fileWriter.write("moocow");
    } finally {
        fileWriter.close();
    }
    assert "moocow".equals(this.fileUtils.readFileToString(this.file.getAbsolutePath()));
}

From source file:com.google.mist.plot.MainActivity.java

private void dumpSystemInfo() {
    File dataDir = getOrCreateSessionDir();
    File target = new File(dataDir, "system_info.txt");

    // Only write system info once.
    if (target.exists()) {
        return;//  www  . ja  va 2  s .  c o m
    }

    try {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        FileWriter fw = new FileWriter(target, true);
        fw.write(String.format("Build.DEVICE: %s\n", Build.DEVICE));
        fw.write(String.format("Build.MODEL: %s\n", Build.MODEL));
        fw.write(String.format("Build.PRODUCT: %s\n", Build.PRODUCT));
        fw.write(String.format("Build.VERSION.SDK_INT: %d\n", Build.VERSION.SDK_INT));
        fw.write(String.format("Screen resolution: %d x %d px\n", size.x, size.y));
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:JSONRequesterWithParser.java

private void Save(VehicleData vehicle, String filePath) {
    //print each vehicle information in file in tabular format
    try {/*from w w w  .  j  a v a  2s  . c om*/
        //File file = new File("E:\\Projects\\Car2Go\\car1.txt"); // just for testing.
        //System.out.println("File Path"+filePath);
        File file = new File(filePath);
        FileWriter fw = null;
        if (!file.exists()) {
            file.createNewFile();
            fw = new FileWriter(file.getAbsoluteFile());
            String header = "Date Time" + "\t" + "Address" + "\t" + "Car Name" + "\t" + "License Plate" + "\t"
                    + "Group" + "\t" + "Latitude" + "\t" + "Longitude" + "\n";
            fw.write(header);
        } else {
            fw = new FileWriter(file.getAbsoluteFile(), true);
            String formatedVehicleData = vehicle.dateTime + "\t" + vehicle.address + "\t" + vehicle.carName
                    + "\t" + vehicle.licensePlate + "\t" + vehicle.group + "\t" + vehicle.latitude + "\t"
                    + vehicle.longitude + "\n";
            fw.write(formatedVehicleData);

        }
        fw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}