Example usage for java.io BufferedWriter write

List of usage examples for java.io BufferedWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:fr.ece.epp.tools.Utils.java

public static void writeScript(String path, String version) {
    String system = System.getProperty("os.name");
    try {/*from www  .  jav  a  2 s  .  c  om*/
        if (system.startsWith("Windows")) {
            File writename = new File(path);
            writename.createNewFile();

            BufferedWriter out = new BufferedWriter(new FileWriter(writename));

            out.write("%~d0\n\r");
            out.write("cd %~dp0\n\r");
            out.write("mvn install -P base," + version);
            out.flush(); // 
            out.close(); // ?  

        } else if (system.startsWith("Linux") || system.startsWith("Mac")) {
            File writename = new File(path);
            writename.createNewFile();

            BufferedWriter out = new BufferedWriter(new FileWriter(writename));

            out.write("#!/bin/sh\n\n");
            out.write("BASEDIR=$(dirname $0)\n");
            out.write("cd $BASEDIR\n");
            out.write("mvn install -P base," + version);
            out.flush(); // 
            out.close(); // ?  
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.egt.core.util.VelocityEngineer.java

public static void write(VelocityContext context, String tempname, String filename) throws Exception {
    Bitacora.trace(VelocityEngineer.class, "write", context, tempname, filename);
    StringWriter sw = write(context, tempname);
    if (sw != null) {
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {/*  ww w  . j a v  a  2 s  . c o m*/
            fileWriter = new FileWriter(filename);
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(sw.toString());
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException ex) {
                    Bitacora.logFatal(ex);
                }
            }
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException ex) {
                    Bitacora.logFatal(ex);
                }
            }
        }
    }
}

From source file:PipedCharacters.java

public static void writeStuff(Writer rawOut) {
    try {// ww  w.  j a  v  a 2  s.c  om
        BufferedWriter out = new BufferedWriter(rawOut);

        String[][] line = { { "Java", "Source", "and", "Support." } };

        for (int i = 0; i < line.length; i++) {
            String[] word = line[i];

            for (int j = 0; j < word.length; j++) {
                out.write(word[j]);
            }

            out.newLine();
        }

        out.flush();
        out.close();
    } catch (IOException x) {
        x.printStackTrace();
    }
}

From source file:mase.MaseEvolve.java

public static Map<String, String> readParams(String[] args) throws Exception {
    // Reads command line parameters to a map
    // Checks the parent order
    Map<String, String> argsPars = new LinkedHashMap<>();
    int parentOrder = 0;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-p")) {
            String par = args[i + 1];
            String[] kv = par.split("=");
            if (kv[0].startsWith("parent")) { // Parent parameter
                if (kv[0].equals("parent")) { // Parent with no number
                    argsPars.put("parent." + parentOrder, kv[1]);
                    parentOrder++;/*  www .  ja  va 2s.  c  om*/
                } else { // Numbered parent
                    String[] split = kv[0].split("\\.");
                    int num = Integer.parseInt(split[1]);
                    if (num != parentOrder) {
                        throw new Exception("Parent out of order: " + par);
                    }
                    argsPars.put("parent." + num, kv[1]);
                    parentOrder++;
                }
            } else { // Not a parent parameter
                argsPars.put(kv[0], kv[1]);
            }
        } else if (args[i].equals("-file")) {
            argsPars.put("parent.0", args[i + 1]);
        }
    }

    // Write command line parameters to a temp file -- the root file
    File tempFile = File.createTempFile("masetemp", ".params", new File("."));
    BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
    for (Entry<String, String> e : argsPars.entrySet()) {
        bw.write(e.getKey() + " = " + e.getValue());
        bw.newLine();
    }
    bw.close();

    // Load all the parameter files
    Map<String, String> pars = new LinkedHashMap<>();
    loadParams(pars, tempFile);
    tempFile.delete();
    return pars;
}

From source file:commonUtils.FunctionUtils.java

public static boolean WriteTextFile(String pathFileText, String fileContent) {
    try {/*from  ww w.ja  va  2 s.  c o  m*/
        File file = new File(pathFileText);

        //if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        //Collecting Data
        String fileDat = "";
        if (!IsNullOrEmpty(fileContent)) {
            String[] listContent = StringUtils.split(fileContent, "\n");
            fileDat += listContent.length + "\n";

            for (String tmpPost : listContent) {
                if (tmpPost != null) {
                    fileDat += tmpPost + "\n";
                }
            }
            //true = append file
            FileWriter fileWritter = new FileWriter(file.getPath(), false);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(fileDat);
            bufferWritter.close();

            return true;
        }
    } catch (IOException ex) {
        return false;
    }
    return false;
}

From source file:es.sotileza.plugin.utils.Utilidades.java

public static void addXml(String xml, String linea) throws IOException {
    FileWriter output = new FileWriter(xml, true);
    BufferedWriter writer = new BufferedWriter(output);
    writer.write(linea + "\n");
    writer.flush();/*from   w w w .  j  a va 2s. c o  m*/
}

From source file:com.testmax.util.FileUtility.java

public static void writeToFile(String path, String text) {
    try {/* w  ww  . ja v  a  2s  .c  o m*/
        // Create file 
        FileWriter fstream = new FileWriter(path);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(text);
        //Close the output stream
        out.close();
    } catch (Exception e) {//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

From source file:com.btbb.figadmin.VanillaBans.java

public static void exportBans(List<EditBan> bans) throws IOException {
    JsonArray mcbans = new JsonArray();
    JsonArray ipbans = new JsonArray();
    for (EditBan b : bans) {
        String created = "", expires = "";
        if (b.time > 0)
            created = mcDateFormat.format(new Date(b.time));
        if (b.endTime > 0)
            expires = mcDateFormat.format(new Date(b.endTime));
        if (b.type == EditBan.BanType.IPBAN && b.ipAddress != null)
            ipbans.add(new IPBan(b.ipAddress, created, b.admin, expires, b.reason).toJson());
        if (b.type == EditBan.BanType.BAN || (b.type == EditBan.BanType.IPBAN && b.ipAddress == null))
            mcbans.add(new MCBan(b.uuid.toString(), b.name, created, b.admin, expires, b.reason).toJson());
    }/*from w w w  .  j  a va  2  s .  c om*/

    GsonBuilder gsonbuilder = (new GsonBuilder()).setPrettyPrinting();
    Gson b = gsonbuilder.create();
    String s = b.toJson(mcbans);
    BufferedWriter bufferedwriter = null;

    try {
        bufferedwriter = Files.newWriter(new File("banned-players.json"), Charsets.UTF_8);
        bufferedwriter.write(s);
    } finally {
        IOUtils.closeQuietly(bufferedwriter);

        s = b.toJson(ipbans);
    }
    try {
        bufferedwriter = Files.newWriter(new File("banned-ips.json"), Charsets.UTF_8);
        bufferedwriter.write(s);
    } finally {
        IOUtils.closeQuietly(bufferedwriter);
    }
}

From source file:com.calamp.services.kinesis.events.writer.CalAmpEventWriter.java

public static void genRandEventsToFile(String filePath, int numToGen) throws IOException {
    // Repeatedly send stock trades with a some milliseconds wait in between
    BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, false));
    for (int i = 0; i < numToGen; i++) {
        CalAmpEvent event = CalAmpEventGenerator.getRandomMessage();
        bw.write(event.toJsonAsString());
        bw.newLine();/*from   www  . j  a  v  a2 s.  com*/
        //System.out.println( event.toJsonAsString() );
        //System.out.println( CalAmpEvent.fromJsonAsString(event.toJsonAsString()) );
    }
    if (bw != null) {
        bw.close();
    }
}

From source file:org.jboss.as.test.manualmode.web.valve.authenticator.ValveUtil.java

public static void createValveModule(final ManagementClient managementClient, String modulename,
        String baseModulePath, String jarName, Class valveClass) throws Exception {
    log.info("Creating a valve module " + modulename);
    String path = ValveUtil.readASPath(managementClient.getControllerClient());
    File file = new File(path);
    if (file.exists()) {
        file = new File(path + baseModulePath);
        file.mkdirs();// w  ww  .j a va2  s  .co  m
        file = new File(path + baseModulePath + "/" + jarName);
        if (file.exists()) {
            file.delete();
        }
        createJar(file, valveClass);
        file = new File(path + baseModulePath + "/module.xml");
        if (file.exists()) {
            file.delete();
        }
        FileWriter fstream = new FileWriter(path + baseModulePath + "/module.xml");
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("<module xmlns=\"urn:jboss:module:1.1\" name=\"" + modulename + "\">\n");
        out.write("    <properties>\n");
        out.write("        <property name=\"jboss.api\" value=\"private\"/>\n");
        out.write("    </properties>\n");

        out.write("    <resources>\n");
        out.write("        <resource-root path=\"" + jarName + "\"/>\n");
        out.write("    </resources>\n");

        out.write("    <dependencies>\n");
        out.write("        <module name=\"sun.jdk\"/>\n");
        out.write("        <module name=\"javax.servlet.api\"/>\n");
        out.write("        <module name=\"org.jboss.as.web\"/>\n");
        out.write("       <module name=\"org.jboss.logging\"/>\n");
        out.write("    </dependencies>\n");
        out.write("</module>");
        out.close();
    }
}