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:eu.europa.ec.markt.dss.validation102853.ValidationResourceManager.java

/**
 * This method saves the data in the output stream to a file.
 *
 * @param diagnosticDataFileName//w w w .  ja  v a2  s  .co m
 * @param outputStream
 * @throws IOException
 */
protected static void saveToFile(final String diagnosticDataFileName, final OutputStream outputStream)
        throws IOException {

    FileWriter file = null;
    try {

        file = new FileWriter(diagnosticDataFileName);
        file.write(outputStream.toString());
    } finally {

        if (file != null) {

            file.close();
        }
    }
}

From source file:fll.web.IntegrationTestUtils.java

public static void storeScreenshot(final WebDriver driver) throws IOException {
    final File baseFile = File.createTempFile("fll", null, new File("screenshots"));

    final File screenshot = new File(baseFile.getAbsolutePath() + ".png");
    if (driver instanceof TakesScreenshot) {
        final File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, screenshot);
        LOGGER.error("Screenshot saved to " + screenshot.getAbsolutePath());
    } else {//from w w w .java 2  s .c  om
        LOGGER.warn("Unable to get screenshot");
    }

    final File htmlFile = new File(baseFile.getAbsolutePath() + ".html");
    final String html = driver.getPageSource();
    final FileWriter writer = new FileWriter(htmlFile);
    writer.write(html);
    writer.close();
    LOGGER.error("HTML saved to " + htmlFile.getAbsolutePath());

}

From source file:com.intuit.tank.proxy.Main.java

static SSLContextSelector getSSLContextSelector() throws GeneralSecurityException, IOException {
    File ks = new File("auto_generated_ca.p12");
    String type = "PKCS12";
    char[] password = "password".toCharArray();
    String alias = "CA";
    if (ks.exists()) {
        try {//from   ww w. j av a 2s . c om
            return new AutoGeneratingContextSelector(ks, type, password, password, alias);
        } catch (GeneralSecurityException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        } catch (IOException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        }
    }
    System.err.println("Generating a new CA");
    X500Principal ca = new X500Principal(
            "cn=OWASP Custom CA for Tank,ou=Tank Custom CA,o=Tank,l=Tank,st=Tank,c=Tank");
    AutoGeneratingContextSelector ssl = new AutoGeneratingContextSelector(ca);
    try {
        ssl.save(ks, type, password, password, alias);
    } catch (GeneralSecurityException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    } catch (IOException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    }
    FileWriter pem = null;
    try {
        pem = new FileWriter("auto_generated_ca.pem");
        pem.write(ssl.getCACert());
    } catch (IOException e) {
        System.err.println("Error writing CA cert : " + e.getLocalizedMessage());
    } finally {
        if (pem != null)
            pem.close();
    }
    return ssl;
}

From source file:com.sakadream.sql.SQLConfigJson.java

/**
 * Save SQLConfig to config.json//from  w w  w .j a va2 s  . com
 * @param sqlConfig SQLConfig object
 * @param encrypt Are you want to encrypt config.json?
 * @throws Exception
 */
public static void save(SQLConfig sqlConfig, Boolean encrypt) throws Exception {
    File file = new File(path);
    if (!file.exists()) {
        file.createNewFile();
    } else {
        FileChannel outChan = new FileOutputStream(file, true).getChannel();
        outChan.truncate(0);
        outChan.close();
    }
    FileWriter writer = new FileWriter(file);
    String json = gson.toJson(sqlConfig, type);
    if (encrypt) {
        Security security = new Security();
        writer.write(security.encrypt(json));
    } else {
        writer.write(json);
    }
    writer.close();
}

From source file:com.sakadream.sql.SQLConfigJson.java

/**
 * Save SQLConfig to custom file name//from   ww w .  ja v  a2 s  . c o  m
 * @param fileName SQLConfig file path
 * @param sqlConfig SQLConfig object
 * @param encrypt Are you want to encrypt config.json?
 * @throws Exception
 */
public static void save(String fileName, SQLConfig sqlConfig, Boolean encrypt) throws Exception {
    fileName = Utilis.changeFileName(fileName, "json");
    File file = new File(fileName);
    if (!file.exists()) {
        file.createNewFile();
    } else {
        FileChannel outChan = new FileOutputStream(file, true).getChannel();
        outChan.truncate(0);
        outChan.close();
    }
    FileWriter writer = new FileWriter(file);
    String json = gson.toJson(sqlConfig, type);
    if (encrypt) {
        Security security = new Security();
        writer.write(security.encrypt(json));
    } else {
        writer.write(json);
    }
    writer.close();
}

From source file:LineageSimulator.java

public static void writeOutputFile(String fileName, String data) {
    try {//from   w w w.j av a  2  s. c om
        FileWriter fw = new FileWriter(fileName);
        fw.write(data);
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("Failed to write to the file: " + fileName);
        System.exit(-1);
    }
}

From source file:Main.java

public static String getMotherboardSN() {
    String result = "";
    try {/*from   w  w w .  jav a2  s  . co  m*/
        File file = File.createTempFile("realhowto", ".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                + "Set colItems = objWMIService.ExecQuery _ \n" + "   (\"Select * from Win32_BaseBoard\") \n"
                + "For Each objItem in colItems \n" + "    Wscript.Echo objItem.SerialNumber \n"
                + "    exit for  ' do the first cpu only! \n" + "Next \n";

        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null) {
            result += line;
        }
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result.trim();
}

From source file:Util.PacketGenerator.java

public static void GenerateTopology() {
    try {//  w w w. j  a  v a  2  s . com
        File file = new File("D:\\Mestrado\\Prototipo\\topologydata\\AS1239\\CONVERTED.POP.1239");
        File newFile = new File("D:\\Mestrado\\SketchMatrix\\trunk\\AS1239_TOPOLOGY");

        FileInputStream topologyFIS = new FileInputStream(file);
        DataInputStream topologyDIS = new DataInputStream(topologyFIS);
        BufferedReader topologyBR = new BufferedReader(new InputStreamReader(topologyDIS));

        String topologyStr = topologyBR.readLine();

        int numberOfEdges = 0;
        Set<Integer> nodes = new HashSet<>();

        while (topologyStr != null) {

            String[] edge = topologyStr.split(" ");

            nodes.add(Integer.parseInt(edge[0]));
            nodes.add(Integer.parseInt(edge[1]));
            numberOfEdges++;

            topologyStr = topologyBR.readLine();

        }
        topologyBR.close();
        topologyDIS.close();
        topologyFIS.close();

        topologyFIS = new FileInputStream(file);
        topologyDIS = new DataInputStream(topologyFIS);
        topologyBR = new BufferedReader(new InputStreamReader(topologyDIS));
        FileWriter topologyFW = new FileWriter(newFile);

        topologyStr = topologyBR.readLine();

        topologyFW.write(nodes.size() + " " + numberOfEdges + "\n");

        while (topologyStr != null) {

            String[] edge = topologyStr.split(" ");

            nodes.add(Integer.parseInt(edge[0]));
            nodes.add(Integer.parseInt(edge[1]));
            nodes.add(Integer.parseInt(edge[1]));

            topologyFW.write(Integer.parseInt(edge[0]) + " " + Integer.parseInt(edge[1]) + " 1\n");

            topologyStr = topologyBR.readLine();

        }

        topologyBR.close();
        topologyDIS.close();
        topologyFIS.close();
        topologyFW.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:au.org.ala.layers.grid.GridCacheBuilder.java

private static void writeGroupHeader(File f, ArrayList<Grid> group) throws IOException {
    FileWriter fw = null;
    try {//from  w w  w . ja va2s .co m
        fw = new FileWriter(f);
        for (int i = 0; i < group.size(); i++) {
            fw.write(group.get(i).filename);
            fw.write("\n");
        }
        fw.flush();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

From source file:net.rptools.maptool.client.ChatAutoSave.java

private static TimerTask createTimer(final long timeout) {
    TimerTask t = new TimerTask() {
        @Override//from  www  .j a va2s .c om
        public void run() {
            if (log.isDebugEnabled())
                log.debug("Chat log autosave countdown complete from " + timeout); //$NON-NLS-1$
            if (chatlog == null) {
                String filename = AppPreferences.getChatFilenameFormat();
                // FJE Ugly kludge to replace older default entry with newer default
                // TODO This is going into 1.3.b77 so remove it in 3-4 builds
                if ("chatlog.html".equals(filename)) { //$NON-NLS-1$
                    AppPreferences.clearChatFilenameFormat();
                    filename = AppPreferences.getChatFilenameFormat();
                }
                chatlog = String.format(filename, new Date()).replace(':', '-');
            }
            File chatFile = new File(AppUtil.getAppHome("autosave").toString(), chatlog); //$NON-NLS-1$
            if (log.isInfoEnabled())
                log.info("Saving log to '" + chatFile + "'"); //$NON-NLS-1$ //$NON-NLS-2$

            FileWriter writer = null;
            CommandPanel chat = MapTool.getFrame().getCommandPanel();
            String old = MapTool.getFrame().getStatusMessage();
            try {
                MapTool.getFrame().setStatusMessage(I18N.getString("ChatAutoSave.status.chatAutosave")); //$NON-NLS-1$
                writer = new FileWriter(chatFile);
                writer.write(chat.getMessageHistory());
                if (log.isInfoEnabled())
                    log.info("Log saved"); //$NON-NLS-1$
            } catch (IOException e) {
                // If this happens should we track it and turn off the autosave?  Perhaps
                // after a certain number of consecutive failures?  Or maybe just lengthen
                // the amount of time between attempts in that case?  At a minimum we
                // should probably give the user a chance to turn it off as part of this
                // message box that pops up...
                MapTool.showWarning("msg.warn.failedAutoSavingMessageHistory", e); //$NON-NLS-1$
            } finally {
                IOUtils.closeQuietly(writer);
                MapTool.getFrame().setStatusMessage(old);
            }
        }
    };
    return t;
}