Example usage for java.io FileWriter FileWriter

List of usage examples for java.io FileWriter FileWriter

Introduction

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

Prototype

public FileWriter(FileDescriptor fd) 

Source Link

Document

Constructs a FileWriter given a file descriptor, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:com.mycompany.asyncreq.GetThread.java

@Override
public void run() {
    try {/*ww  w .j  a v  a  2  s. c  o m*/
        Future<HttpResponse> future = client.execute(request, context, null);
        HttpResponse response = future.get();
        MatcherAssert.assertThat(response.getStatusLine().getReasonPhrase(), equals(200));
        Boolean isDone = true;
        Scanner scan = new Scanner(System.in);
        File f = new File("my.txt");
        FileWriter fr = new FileWriter(f);
        BufferedWriter bwr = new BufferedWriter(fr);
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
        while ((br.readLine()) != null) {

            bwr.write(new Scanner(System.in).nextLine());

        }
    } catch (Exception ex) {
        System.out.println(ex.getLocalizedMessage());
    }
}

From source file:net.sf.reportengine.out.TestHtmlReportOutput.java

/**
 * Test method for/*ww w. j av a  2 s .  co m*/
 * {@link net.sf.reportengine.out.AbstractFreemarkerReportOutput#close()}.
 */
@Test
public void testCloseWriterException() {
    try {
        FileWriter fileWriter = new FileWriter("./target/TestClosingWriterException.html");
        HtmlReportOutput classUnderTest = new HtmlReportOutput(fileWriter, true);
        classUnderTest.open();
        classUnderTest.output("emptyLine.ftl");
        classUnderTest.close();

        // at this point the writer should be already closed
        fileWriter.write("if you see this the test has failed");
        fail("An IOException should have been thrown at this point");
    } catch (IOException e) {
        assertEquals("Stream closed", e.getMessage());
    }
}

From source file:edu.caltech.ipac.firefly.server.util.DsvToDataGroup.java

public static void write(File outf, DataGroup data) throws IOException {
    write(new FileWriter(outf), data, CSVFormat.DEFAULT);
}

From source file:de.wdilab.coma.gui.extensions.RemoteRepo.java

public static boolean getRemoteConnection(String s, UUID uid) {

    UUID uuid;/*ww  w  .j ava2s .  co  m*/
    String xslt_text;
    boolean is_stored = false;

    xslt_text = s.replaceAll("\"", "\\\"");
    xslt_text = xslt_text.replaceAll("\'", "\\\'");
    uuid = uid;

    try {
        File f = new File("helpingFile.csv");
        PrintWriter output = new PrintWriter(new FileWriter(f));
        output.println("uuid,data");
        xslt_text.trim();
        xslt_text = xslt_text.replace("\n", "").replace("\r", "");
        output.println(uuid + "," + xslt_text);
        output.close();

        HttpClient client = new DefaultHttpClient();
        FileEntity entity = new FileEntity(f, ContentType.create("text/plain", "UTF-8"));

        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(entity);
        HttpResponse response = client.execute(httppost);
        //            System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200)
            is_stored = true;
        f.delete();
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

    //        System.out.println("xslt stored   "+is_stored);

    return is_stored;

}

From source file:IOUtils.java

public static ContentHandler getSerializer(File file) throws IOException, TransformerException {
    final FileWriter writer = new FileWriter(file);

    final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
    final Transformer transformer = transformerHandler.getTransformer();

    final Properties format = new Properties();
    format.put(OutputKeys.METHOD, "xml");
    format.put(OutputKeys.OMIT_XML_DECLARATION, "no");
    format.put(OutputKeys.ENCODING, "UTF-8");
    format.put(OutputKeys.INDENT, "yes");
    transformer.setOutputProperties(format);

    transformerHandler.setResult(new StreamResult(writer));

    try {//from  ww  w . j a v  a 2s. c  o m
        if (needsNamespacesAsAttributes(format)) {
            return new NamespaceAsAttributes(transformerHandler);
        }
    } catch (SAXException se) {
        throw new TransformerException("Unable to detect of namespace support for sax works properly.", se);
    }
    return transformerHandler;
}

From source file:test.gov.nih.nci.system.web.client.OrderLineCreateClient.java

private static File marshall(OrderLine line) throws IOException, XMLUtilityException {
    String namespacePrefix = "gme://caCORE.caCORE/3.2/";
    String jaxbContextName = "gov.nih.nci.cacoresdk.domain.onetoone.bidirectional";
    Marshaller marshaller = new JAXBMarshaller(true, jaxbContextName, namespacePrefix);
    Unmarshaller unmarshaller = new JAXBUnmarshaller(true, jaxbContextName);
    XMLUtility myUtil = new XMLUtility(marshaller, unmarshaller);
    File myFile = new File("OrderLine.xml");
    FileWriter myWriter = new FileWriter(myFile);
    myUtil.toXML(line, myWriter);/*from   w  ww.j a  v a2 s.  c o  m*/
    myWriter.close();
    return myFile;
}

From source file:Cursling.deleteMonitor.java

public String _deleteMonitor(String monitor_name) {
    isdeleted = false;/*  w ww .ja  v a  2  s  .c  o  m*/
    try {
        File inFile = new File(input_file_del);

        if (!inFile.isFile()) {
            logger.info("Failed to Delete. Data file not found.");
            return ("failed");
        }
        storeUpdate = new File(input_file_del);
        File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
        BufferedReader br = new BufferedReader(new FileReader(input_file_del));
        PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
        String line = null;

        while ((line = br.readLine()) != null) {
            if (!line.equalsIgnoreCase("")) {
                String tmp_linee = line.replaceAll("\\\\", "");
                if (tmp_linee.startsWith(monitor_name + " = ht")
                        || tmp_linee.startsWith(monitor_name + "=ht")) {
                    isdeleted = true;
                    pw.flush();
                } else if (tmp_linee.startsWith(monitor_name.toLowerCase() + " = ena")
                        || tmp_linee.startsWith(monitor_name.toLowerCase() + " = dis")
                        || tmp_linee.startsWith(monitor_name.toLowerCase() + "=ena")
                        || tmp_linee.startsWith(monitor_name.toLowerCase() + "=dis")) {
                    isdeleted = true;
                    pw.flush();
                } else {
                    pw.println(line);
                    pw.flush();
                }

            } else {
            }
        }

        pw.close();
        br.close();

        //Rename the new file to the filename the original file had.
        if (tempFile.canRead()) {
            Long before = storeUpdate.lastModified();
            FileUtils.copyFile(tempFile, storeUpdate);
            Long after = storeUpdate.lastModified();
            if (before.equals(after)) {
                logger.info("failed to delete Monitor " + monitor_name);
                return ("failed");
            } else if (isdeleted == true && deleteOutput(monitor_name) == true) {
                logger.info("Monitor " + monitor_name + " Deleted.");
                return ("deleted");
            } else if (isdeleted == true && deleteOutput(monitor_name) == false) {
                logger.info("Monitor " + monitor_name + " Deleted. But output will removed in Sometime.");
                return ("deleted");
            } else {
                logger.info("Failed to delete Monitor " + monitor_name + " . Monitor not found.");
                return ("not_found");
            }
        } else {
            logger.info("failed to delete Monitor " + monitor_name);
            return ("failed");
        }
    } catch (FileNotFoundException ex) {
        logger.info("failed to delete Monitor " + monitor_name);
        logger.error("Error1 : " + ex);
        return ("failed");
    } catch (IOException ex) {
        logger.info("failed to delete Monitor " + monitor_name);
        logger.error("Error2 : " + ex);
        return ("failed");
    }
}

From source file:Cursling.deleteNotification.java

public String _doDelete(String notification_name) {
    isdeleted = false;/* w  w  w .j a  v  a 2 s. co  m*/
    try {
        File inFile = new File(input_notification_file_del);

        if (!inFile.isFile()) {
            logger.info("Failed to Delete. Data file not found.");
            return ("failed");
        }
        storeUpdate = new File(input_notification_file_del);
        File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
        BufferedReader br = new BufferedReader(new FileReader(input_notification_file_del));
        PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
        String line = null;

        while ((line = br.readLine()) != null) {
            if (!line.equalsIgnoreCase("")) {
                if (line.startsWith(notification_name + "=") || line.startsWith(notification_name + " =")) {
                    isdeleted = true;
                    pw.flush();
                } else {
                    pw.println(line);
                    pw.flush();
                }
            } else { //do nothing
            }
        }
        pw.close();
        br.close();

        //Rename the new file to the filename the original file had.
        if (tempFile.canRead()) {
            Long before = storeUpdate.lastModified();
            FileUtils.copyFile(tempFile, storeUpdate);
            Long after = storeUpdate.lastModified();
            if (before.equals(after)) {
                logger.info("failed to delete Notification Group " + notification_name);
                return ("failed");
            } else if (isdeleted == true) {
                logger.info("Notification Group " + notification_name + " Deleted.");
                obj_convert._doConvert();
                return ("deleted");
            } else {
                logger.info("Failed to delete Notification Group " + notification_name
                        + " . Notification Group not found.");
                return ("not_found");
            }
        } else {
            logger.info("failed to delete Notification Group " + notification_name);
            return ("failed");
        }
    } catch (FileNotFoundException ex) {
        logger.info("failed to delete Notification Group " + notification_name);
        logger.error("Error : " + ex);
        return ("failed");
    } catch (IOException ex) {
        logger.info("failed to delete Notification Group " + notification_name);
        logger.error("Error : " + ex);
        return ("failed");
    }
}

From source file:model.Post_store.java

public static int getlastid() {
    JSONParser parser = new JSONParser();
    int lastid = 1;
    try {/*from   ww  w.  jav a  2  s. c  o m*/

        Object idObj = parser.parse(new FileReader(root + "posts/lastid.json"));

        JSONObject jsonObject = (JSONObject) idObj;

        String slastid = jsonObject.get("lastid").toString();
        lastid = Integer.parseInt(slastid);

        //System.out.println(lastid);

    } catch (FileNotFoundException e) {
        JSONObject newIdObj = new JSONObject();
        lastid = 1;
        newIdObj.put("lastid", lastid);

        try (FileWriter file = new FileWriter(root + "posts/lastid.json");) {
            file.write(newIdObj.toJSONString());
            file.flush();
            file.close();

        } catch (IOException ex) {
            System.out.println(e);
        }
    } catch (IOException e) {
        System.out.println(e);
    } catch (ParseException e) {
        System.out.println(e);
    }

    return lastid;
}

From source file:com.bekwam.examples.javafx.oldscores.SettingsDAOImpl.java

@Override
public void save() throws IOException {

    if (logger.isDebugEnabled()) {
        logger.debug("[SAVE] saving " + getAbsolutePath());
    }/*from   www. j  a v a2  s .c  o m*/
    FileWriter fw = new FileWriter(getAbsolutePath());
    Properties props = new Properties();
    props.setProperty("oldscores.roundUp", String.valueOf(settings.getRoundUp()));
    props.store(fw, "");
    fw.close();
}