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:aes.pica.touresbalon.tb_serviciosbolivariano.ServiciosBolivarianos.java

public static void EcribirFichero(File Ffichero, String SCadena) {
    try {/*  www .  j  a  v  a  2  s .  c om*/
        //Si no Existe el fichero lo crea
        if (!Ffichero.exists()) {
            //Ffichero.createNewFile();
        }
        /*Abre un Flujo de escritura,sobre el fichero con codificacion utf-8. 
        *Adems  en el pedazo de sentencia "FileOutputStream(Ffichero,true)",
        *true es por si existe el fichero seguir aadiendo texto y no borrar lo que tenia*/
        BufferedWriter Fescribe = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(Ffichero, true), "utf-8"));
        /*Escribe en el fichero la cadena que recibe la funcin. 
        *el string "\r\n" significa salto de linea*/
        Fescribe.write(SCadena + "\r\n");
        //Cierra el flujo de escritura
        Fescribe.close();
    } catch (Exception ex) {
        //Captura un posible error le imprime en pantalla 
        System.out.println(ex.getMessage());
    }
}

From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java

public static <T> void collection2Text(Collection<T> c, String filename, String header) {
    BufferedWriter bw = IOUtils.getBufferedWriter(filename);
    try {/* w  w  w .j  a  v  a 2 s  .c  o m*/
        if (header != null) {
            bw.write(header);
            bw.newLine();
        }
        for (Iterator<T> iterator = c.iterator(); iterator.hasNext();) {

            bw.write(iterator.next().toString());
            bw.newLine();
        }
        bw.flush();
        bw.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.thejustdo.util.Utils.java

/**
 * Creates a new temporal file with the content given in the string.
 * @param content What to write son of a bitch.
 */// w  w  w  .j  av  a2  s  .co  m
public static void createTMPFile(String content, File goal) throws IOException {
    // 1. Creating the writers.
    FileOutputStream fos = new FileOutputStream(goal);
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "ISO-8859-1"));

    // 2. Writing contents.
    writer.write(content);

    // 3. Closing stream.
    writer.flush();
    writer.close();
    fos.close();
}

From source file:elh.eus.absa.NLPpipelineWrapper.java

public static int eustaggerCall(String taggerCommand, String string, String fname) {

    try {/*from  w  w  w  . j  av a  2  s . com*/
        File temp = new File(fname);
        //System.err.println("eustaggerCall: created temp file: "+temp.getAbsolutePath());
        BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
        bw.write(string + "\n");
        bw.close();

        String[] command = { taggerCommand, temp.getName() };
        System.err.println("Eustagger agindua: " + Arrays.toString(command));

        ProcessBuilder eustBuilder = new ProcessBuilder().command(command);
        eustBuilder.directory(new File(temp.getParent()));
        //.redirectErrorStream(true);
        Process eustagger = eustBuilder.start();
        int success = eustagger.waitFor();
        //System.err.println("eustagger succesful? "+success);
        if (success != 0) {
            System.err.println("eustaggerCall: eustagger error");
        } else {
            String tagged = fname + ".kaf";
            BufferedReader reader = new BufferedReader(new InputStreamReader(eustagger.getInputStream()));
            //new Eustagger_lite outputs to stdout. Also called ixa-pipe-pos-eu
            if (taggerCommand.contains("eustagger") || taggerCommand.contains("ixa-pipe")) {
                Files.copy(eustagger.getInputStream(), Paths.get(tagged));
            }
            // old eustagger (euslem)
            else {
                FileUtilsElh.renameFile(temp.getAbsolutePath() + ".etiketatua3", tagged);
            }
        }
        //
        // delete all temporal files used in the process.
        temp.delete();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return -1;
    }

    return 0;
}

From source file:Main.java

public static void saveUtfFileWithBOM(File file, String content) throws IOException {
    BufferedWriter bw = null;
    OutputStreamWriter osw = null;

    FileOutputStream fos = new FileOutputStream(file);
    try {/*from w w w .j ava 2  s.  c om*/
        // write UTF8 BOM mark if file is empty
        if (file.length() < 1) {
            final byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
            fos.write(bom);
        }

        osw = new OutputStreamWriter(fos, "UTF-8");
        bw = new BufferedWriter(osw);
        if (content != null) {
            bw.write(content);
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        try {
            bw.close();
            fos.close();
        } catch (Exception ex) {
        }
    }
}

From source file:cloudclient.Client.java

public static void batchReceiveResp(BufferedReader in) throws IOException, ParseException {
    BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));

    JSONParser parser = new JSONParser();

    String message;//from  ww  w . jav  a2  s.com
    while ((message = in.readLine()) != null) {
        //System.out.println(message);
        JSONArray responseList = (JSONArray) parser.parse(message);

        for (int i = 0; i < responseList.size(); i++) {
            JSONObject response = (JSONObject) responseList.get(i);
            bw.write(response.get("URL").toString());
            bw.newLine();
        }
    }

    bw.close();

}

From source file:com.gargoylesoftware.htmlunit.source.JQuery173Extractor.java

/**
 * Transforms the raw expectation, to the needed one by HtmlUnit.
 * This methods puts only the main line of the test output, without the details.
 *
 * @param input the raw file of real browser, with header and footer removed
 * @param output the expectation/* www  .  ja  v  a  2  s.co  m*/
 * @throws IOException if an error occurs
 */
public static void extractExpectations(final File input, final File output) throws IOException {
    final BufferedReader reader = new BufferedReader(new FileReader(input));
    final BufferedWriter writer = new BufferedWriter(new FileWriter(output));
    int testNumber = 1;
    String line;
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("" + testNumber + '.') && line.endsWith("Rerun")) {
            line = line.substring(0, line.length() - 5);
            writer.write(line + "\n");
            testNumber++;
        }
    }
    System.out.println("Last output #" + (testNumber - 1));
    reader.close();
    writer.close();
}

From source file:io.jari.geenstijl.API.API.java

public static String postUrl(String url, List<NameValuePair> params, String cheader, boolean refererandorigin)
        throws IOException {
    HttpURLConnection http = (HttpURLConnection) new URL(url).openConnection();
    http.setRequestMethod("POST");
    http.setDoInput(true);// w  ww. j av  a 2s  . com
    http.setDoOutput(true);
    if (cheader != null)
        http.setRequestProperty("Cookie", cheader);
    if (refererandorigin) {
        http.setRequestProperty("Referer",
                "http://www.geenstijl.nl/mt/archieven/2014/01/brein_chanteert_ondertitelaars.html");
        http.setRequestProperty("Origin", "http://www.geenstijl.nl");
    }
    OutputStream os = http.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
    writer.write(getQuery(params));
    writer.flush();
    writer.close();
    os.close();

    http.connect();

    InputStream in = http.getInputStream();
    String encoding = http.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    int len = 0;
    while ((len = in.read(buf)) != -1) {
        baos.write(buf, 0, len);
    }
    return new String(baos.toByteArray(), encoding);
}

From source file:com.hmiard.blackwater.projects.Builder.java

/**
 * Deleting a server from a project./* ww w .j  av  a 2 s . c  o m*/
 *
 * @param projectRoot String
 * @param serverName String
 * @param listener ConsoleEmulator
 * @return Boolean
 */
public static Boolean deleteServer(String projectRoot, String serverName, ConsoleEmulator listener) {

    serverName = serverName.substring(0, 1).toUpperCase() + serverName.substring(1).toLowerCase();
    serverName += "Server";
    File checker = new File(projectRoot + "\\src\\" + serverName);
    File composerJSON = new File(projectRoot + "\\composer.json");

    if (!checker.exists()) {
        listener.push("This server is missing ! Operation aborted.");
        return false;
    }
    if (!composerJSON.exists()) {
        listener.push("File composer.json is missing ! Operation aborted.");
        return false;
    }

    try {
        FileUtils.deleteDirectory(checker);

        if (checker.exists()) {
            listener.push("Woops ! It seems impossible to delete " + checker.getAbsolutePath());
            return false;
        }

        JSONObject composer = new JSONObject(readFile(composerJSON.getAbsolutePath()));
        JSONObject autoload = composer.getJSONObject("autoload");
        JSONObject psr0 = autoload.getJSONObject("psr-0");

        psr0.remove(serverName);

        BufferedWriter cw = new BufferedWriter(new FileWriter(composerJSON.getAbsoluteFile()));
        String content = composer.toString(4).replaceAll("\\\\", "");
        cw.write(content);
        cw.close();

        listener.push("\n" + serverName + " was deleted successfully !\n");

    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }

    return true;
}

From source file:com.dexels.navajo.tipi.dev.server.appmanager.impl.UnsignJarTask.java

private static void cleanManifest(File manifest, File outputManifest, List<String> extraHeaders)
        throws IOException {

    BufferedReader fr = null;// w w w.  ja  v a 2s.c om
    List<StringBuffer> manifestheaders;
    try {
        fr = new BufferedReader(new FileReader(manifest));
        String line = null;
        manifestheaders = new ArrayList<StringBuffer>();
        StringBuffer current = null;
        do {
            line = fr.readLine();
            if (line == null) {
                continue;
            }
            if (line.startsWith(" ")) {
                current.append(line);
                current.append("\n");
            } else {
                if (current != null) {
                    manifestheaders.add(current);
                }
                current = new StringBuffer();
                current.append(line.trim());
                current.append("\n");
            }
        } while (line != null);
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (Exception e) {
            }
        }
    }
    BufferedWriter fw = null;
    try {
        fw = new BufferedWriter(new FileWriter(outputManifest));
        List<StringBuffer> output = new ArrayList<StringBuffer>();

        for (StringBuffer header : manifestheaders) {
            if (checkLine(header.toString())) {
                output.add(header);
                fw.write(header.toString());
            }
        }
        for (String header : extraHeaders) {
            fw.write(header + "\n");
        }
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception e) {
            }
        }
    }

}