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:Messenger.TorLib.java

/**
 * This method makes a http GET request for the specified resource to the specified hostname.
 * It uses the SOCKS proxy to a connection over Tor.
 * The DNS lookup is also done over Tor.
 * This method only uses port 443 for SSL.
 *
 * @param hostname hostname for target server.
 * @param port port to connect to./*from  www  .ja  va  2s  .com*/
 * @param resource resource to lookup with GET request.
 * @return returns a JSON object.
 * @throws IOException
 * @throws JSONException
 */
public static JSONObject getJSON(String hostname, int port, String resource)
        throws IOException, JSONException, HttpException {
    //Create a SSL socket using Tor
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);
    sslSocket.startHandshake();
    openSockets.add(sslSocket);

    //Create the HTTP GET request and push it over the outputstream
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("GET /" + resource + " HTTP/1.0\r\n");
    wr.write("Host: " + hostname + "\r\n");
    wr.write("\r\n");
    wr.flush();

    //Listen for a response on the inputstream
    BufferedReader br = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String t;
    boolean start = false;
    String output = "";
    while ((t = br.readLine()) != null) {
        if (t.equals("")) {
            start = true;
        }
        if (start) {
            output = output + t;
        }
    }
    br.close();
    wr.close();
    sslSocket.close();
    System.out.println(output);
    openSockets.remove(sslSocket);
    return new JSONObject(output);
}

From source file:Main.java

/**
 * Converts a plain text file into TE3-input file
 *
 * @param plainfile/*  w  w  w  .  j  a  va  2  s .co m*/
 * @return
 */
public static String Plain2TE3(String plainfile) {
    String outputfile = null;
    try {
        String line;
        boolean textfound = false;
        String header = "";
        String footer = "";
        String text = "";

        //process header (and dct)/text/footer
        outputfile = plainfile + ".TE3input";
        BufferedWriter te3writer = new BufferedWriter(new FileWriter(new File(outputfile)));
        BufferedReader inputReader = new BufferedReader(new FileReader(new File(plainfile)));

        try {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dctvalue = sdf.format(new Date());

            te3writer.write("<?xml version=\"1.0\" ?>");
            te3writer.write(
                    "\n<TimeML xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://timeml.org/timeMLdocs/TimeML_1.2.1.xsd\">\n");
            te3writer.write("\n<DOCID>" + (new File(plainfile)).getName() + "</DOCID>\n");
            te3writer.write("\n<DCT><TIMEX3 tid=\"t0\" type=\"DATE\" value=\"" + dctvalue
                    + "\" temporalFunction=\"false\" functionInDocument=\"CREATION_TIME\">" + dctvalue
                    + "</TIMEX3></DCT>\n");

            // read out text
            while ((line = inputReader.readLine()) != null) {
                text += line + "\n";
            }

            te3writer.write("\n<TEXT>\n" + text + "</TEXT>\n");
            te3writer.write("</TimeML>\n");

        } finally {
            if (inputReader != null) {
                inputReader.close();
            }
            if (te3writer != null) {
                te3writer.close();
            }
        }
    } catch (Exception e) {
        System.err.println("Errors found (TML_file_utils):\n\t" + e.toString() + "\n");
        if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) {
            e.printStackTrace(System.err);
        }
        return null;
    }
    return outputfile;
}

From source file:com.android.dumprendertree2.FsUtils.java

public static void saveTestListToStorage(File file, int start, List<String> testList) {
    try {//from www  .  j av a  2  s  .c  o  m
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        for (String line : testList.subList(start, testList.size())) {
            writer.write(line + '\n');
        }
        writer.flush();
        writer.close();
    } catch (IOException e) {
        Log.e(LOG_TAG, "failed to write test list", e);
    }
}

From source file:biospectra.BioSpectra.java

private static void simulateReads(String[] args) throws Exception {
    String fastaDir = args[0];/*  www.  jav a2 s  .  com*/
    int readSize = Integer.parseInt(args[1]);
    double errorRatioStart = Double.parseDouble(args[2]);
    double errorRatioEnd = Double.parseDouble(args[3]);
    double errorRatioStep = Double.parseDouble(args[4]);
    int iteration = Integer.parseInt(args[5]);
    String outDir = args[6];

    List<File> fastaDocs = FastaFileHelper.findFastaDocs(fastaDir);

    File outDirFile = new File(outDir);
    if (!outDirFile.exists()) {
        outDirFile.mkdirs();
    }

    MetagenomicReadGenerator generator = new MetagenomicReadGenerator(fastaDocs);
    for (double cur = errorRatioStart; cur <= errorRatioEnd; cur += errorRatioStep) {
        File outFile = new File(outDir + "/sample_" + cur + ".fa");
        BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
        for (int i = 0; i < iteration; i++) {
            FASTAEntry sample = generator.generate(readSize, cur);
            bw.write(sample.getHeaderLine());
            bw.newLine();
            bw.write(sample.getSequence());
            bw.newLine();
        }
        bw.close();
    }
}

From source file:com.germinus.easyconf.FileUtil.java

public static void write(File file, String s) throws IOException {
    if (file.getParent() != null) {
        mkdirs(file.getParent());// w w  w  .  j av a2s. c o  m
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    bw.flush();
    bw.write(s);
    bw.flush();

    bw.close();
}

From source file:com.csipsimple.backup.SipProfileJson.java

/**
 * Save current sip configuration/*w  w  w  .  j  av  a2  s. c  om*/
 * 
 * @param ctxt
 * @return
 */
public static boolean saveSipConfiguration(Context ctxt, String filePassword) {
    File dir = PreferencesWrapper.getConfigFolder(ctxt);
    if (dir != null) {
        Date d = new Date();
        File file = new File(dir.getAbsoluteFile() + File.separator + "backup_"
                + DateFormat.format("yy-MM-dd_kkmmss", d) + ".json");
        Log.d(THIS_FILE, "Out dir " + file.getAbsolutePath());

        JSONObject configChain = new JSONObject();
        try {
            configChain.put(KEY_ACCOUNTS, serializeSipProfiles(ctxt));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add profiles", e);
        }
        try {
            configChain.put(KEY_SETTINGS, serializeSipSettings(ctxt));
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add profiles", e);
        }

        try {
            // Create file
            OutputStream fos = new FileOutputStream(file);
            if (!TextUtils.isEmpty(filePassword)) {
                Cipher c;
                try {
                    c = Cipher.getInstance("AES");
                    SecretKeySpec k = new SecretKeySpec(filePassword.getBytes(), "AES");
                    c.init(Cipher.ENCRYPT_MODE, k);
                    fos = new CipherOutputStream(fos, c);
                } catch (NoSuchAlgorithmException e) {
                    Log.e(THIS_FILE, "NoSuchAlgorithmException :: ", e);
                } catch (NoSuchPaddingException e) {
                    Log.e(THIS_FILE, "NoSuchPaddingException :: ", e);
                } catch (InvalidKeyException e) {
                    Log.e(THIS_FILE, "InvalidKeyException :: ", e);
                }
            }
            FileWriter fstream = new FileWriter(file.getAbsoluteFile());
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(configChain.toString(2));
            // Close the output stream
            out.close();
            return true;
        } catch (Exception e) {
            // Catch exception if any
            Log.e(THIS_FILE, "Impossible to save config to disk", e);
            return false;
        }
    }
    return false;
}

From source file:com.hazelcast.qasonar.utils.Utils.java

public static void writeToFile(String fileName, StringBuilder content) throws IOException {
    FileWriter fileWriter = null;
    BufferedWriter writer = null;
    try {/*from www  . j a v  a2s  . c  om*/
        File file = new File(fileName);
        fileWriter = new FileWriter(file);
        writer = new BufferedWriter(fileWriter);
        writer.write(content.toString());
        writer.flush();
    } finally {
        closeQuietly(writer);
        closeQuietly(fileWriter);
    }
}

From source file:com.incapture.rapgen.output.OutputWriter.java

/**
 * Some files are composed of multiple templates. So the map passed in here is filename to template order to template.
 * E.g. "file.txt"->1->"some code" "file.txt"->2->"other code" and so on.
 *
 * @param rootFolder//  ww w  . jav a2  s  .  c  o m
 * @param pathToTemplate
 */
public static void writeMultiPartTemplates(String rootFolder,
        Map<String, Map<String, StringTemplate>> pathToTemplate) {
    // For each file, dump the templates
    for (Map.Entry<String, Map<String, StringTemplate>> entry : pathToTemplate.entrySet()) {
        File file = new File(rootFolder, entry.getKey());
        file.getParentFile().mkdirs();

        BufferedWriter bow = null;
        try {
            bow = new BufferedWriter(new FileWriter(file));
            Set<String> sections = entry.getValue().keySet();
            SortedSet<String> sorted = new TreeSet<String>();
            sorted.addAll(sections);
            for (String sec : sorted) {
                bow.write(entry.getValue().get(sec).toString());
                bow.newLine();
            }
            bow.close();
        } catch (IOException e) {
            System.err.println(e.getMessage());
        } finally {
            if (bow != null) {
                try {
                    bow.close();
                } catch (IOException e) {
                    System.err.println("Error closing output stream: " + ExceptionToString.format(e));
                }
            }
        }
    }
}

From source file:com.kdmanalytics.toif.adaptor.SplintAdaptor.java

public static void writeToFile(String sb) throws IOException {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    File tempFile = new File(tempDir, "toifLog");
    FileWriter fileWriter = new FileWriter(tempFile, true);
    BufferedWriter bw = new BufferedWriter(fileWriter);
    bw.write(sb);
    bw.close();//from   w w w. j ava2 s .co  m
}

From source file:io.cslinmiso.line.utils.Utility.java

/**
 * Java IO (java io write file)//  w w  w  . j a va  2 s.  co m
 * 
 * @param data
 * @param fileName
 * @throws IOException
 */
public static void writeFile(List<String> data, String fileName) throws IOException {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));
    try {
        for (String d : data) {
            bw.write(d);
            bw.newLine();
        }
        bw.flush();
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        bw.close();
    }
}