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:ch.ethz.matsim.ivt_baseline.counts.StreetCountsLinkIdentification.java

private static void writeStreetCounts(String pathToCountsOutput, Map<CountInput, Id<Link>> identifiedLinks) {
    BufferedWriter writer = IOUtils.getBufferedWriter(pathToCountsOutput, Charset.forName("UTF-8"));
    try {/*from   w  w  w  . j  av  a2s  .  c o m*/
        String header = "countStationId" + COUNTS_DELIMITER + "direction" + COUNTS_DELIMITER + "linkId";
        writer.write(header);
        writer.newLine();
        //for (Id<Link> linkId : identifiedLinks.keySet()) {
        for (CountInput countInput : identifiedLinks.keySet()) {
            writer.write(countInput.id + COUNTS_DELIMITER);
            writer.write(countInput.directionDescr + COUNTS_DELIMITER);
            writer.write(identifiedLinks.get(countInput).toString());
            writer.newLine();
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:iics.Connection.java

static void create_file() {
    try {/*from  w  w w . j a v  a 2  s .  c  om*/
        if (f.exists()) {
            content = "<html><head>"
                    + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"> "
                    + "</head><body onload='document.form1.submit()'>\n"
                    + "<form id='form1' name='form1' method='post' action='" + "http://" + path + "/IICS/"
                    + "/'>\n" + "<input type='type' name='s' value='" + IV.toString() + "' />\n"
                    + "<input type='type' name='s1' value='" + encryptionKey.toString() + "' />\n"
                    + "<input type='type' name='b' value='" + Base64Enc.encode((IV.toString() + "~~~"
                            + dt.getLHost().get(1) + "~~~" + det + "~~~" + encryptionKey.toString()).trim())
                    + "' />\n";

            content = content + "</form>\n" + "</body></html>";
            fw = new FileWriter(f.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

        } else {

            System.out.println("No such file exists, creating now");
            success = f.createNewFile();
            if (success) {
                System.out.printf("Successfully created new file: %s%n", f);
            } else {
                //  System.out.printf("Failed to create new file: %s%n", f);
                JOptionPane.showMessageDialog(null,
                        "                 An error occured!! \n Contact your system admin for help.", null,
                        JOptionPane.WARNING_MESSAGE);
                close_loda();
            }

        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null,
                "                 An error occured!! \n Contact your system admin for help.", null,
                JOptionPane.WARNING_MESSAGE);
        close_loda();
    }
}

From source file:it.moondroid.chatbot.alice.Sraix.java

public static void log(String pattern, String template) {
    System.out.println("Logging " + pattern);
    template = template.trim();/* w ww  .  j  a v a2s. c om*/
    if (MagicBooleans.cache_sraix)
        try {
            if (!template.contains("<year>") && !template.contains("No facilities")) {
                template = template.replace("\n", "\\#Newline");
                template = template.replace(",", MagicStrings.aimlif_split_char_name);
                template = template.replaceAll("<a(.*)</a>", "");
                template = template.trim();
                if (template.length() > 0) {
                    FileWriter fstream = new FileWriter("c:/ab/bots/sraixcache/aimlif/sraixcache.aiml.csv",
                            true);
                    BufferedWriter fbw = new BufferedWriter(fstream);
                    fbw.write("0," + pattern + ",*,*," + template + ",sraixcache.aiml");
                    fbw.newLine();
                    fbw.close();
                }
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
}

From source file:edu.cmu.cs.lti.ark.fn.identification.latentmodel.TrainBatchModelDerThreaded.java

/**
 * Writes the current parameters to modelFile, one param per line.
 *
 * @param modelFile the filename to write to
 */// w  w w . j ava 2s  .c  o m
private static void saveModel(double[] params, String modelFile) throws IOException {
    final BufferedWriter bWriter = new BufferedWriter(new FileWriter(modelFile));
    try {
        for (double param : params)
            bWriter.write(param + "\n");
    } finally {
        closeQuietly(bWriter);
    }
}

From source file:common.Utilities.java

public static boolean writeStringToFile(String filename, String stringToWrite) {
    try {/*  w  w w . ja v  a  2  s  .  com*/
        FileWriter writer = new FileWriter(new File(filename));
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write(stringToWrite);
        bw.flush();
        bw.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.example.makerecg.NetworkUtilities.java

/**
 * Connects to the SampleSync test server, authenticates the provided
 * username and password./*from w w w.  j  a va  2  s  .com*/
 * This is basically a standard OAuth2 password grant interaction.
 *
 * @param username The server account username
 * @param password The server account password
 * @return String The authentication token returned by the server (or null)
 */
public static String authenticate(String username, String password) {
    String token = null;

    try {

        Log.i(TAG, "Authenticating to: " + AUTH_URI);
        URL urlToRequest = new URL(AUTH_URI);
        HttpURLConnection conn = (HttpURLConnection) urlToRequest.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("grant_type", "password"));
        params.add(new BasicNameValuePair("client_id", "CLIENT_ID"));
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("password", password));

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(params));
        writer.flush();
        writer.close();
        os.close();

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String response = "";
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
            br.close();

            // Response body will look something like this:
            // {
            //    "token_type": "bearer",
            //    "access_token": "0dd18fd38e84fb40e9e34b1f82f65f333225160a",
            //    "expires_in": 3600
            //  }

            JSONObject jresp = new JSONObject(new JSONTokener(response));

            token = jresp.getString("access_token");
        } else {
            Log.e(TAG, "Error authenticating");
            token = null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        Log.v(TAG, "getAuthtoken completing");
    }

    return token;

}

From source file:css.variable.converter.CSSVariableConverter.java

/**
 * Go through existing CSS Files and replace variable access with variable
 * values/*ww w  .j a  va2 s  .  c  om*/
 *
 * @param rootCSSVars All css variables you want to convert from name to value
 */
private static void editForRelease(ArrayList<CSSVar> rootCSSVars) {
    for (File cssFile : theCSSList) {

        ArrayList<CSSVar> localCSSVars = getCSSVars(cssFile, ":local");
        for (CSSVar cssVar : rootCSSVars) {
            if (!localCSSVars.contains(cssVar)) {
                localCSSVars.add((CSSVar) (cssVar.clone()));
            }
        }

        // This will store info the new text for the file we will write below
        ArrayList<String> filesNewInfo = new ArrayList<String>();
        try {

            Scanner fileReader = new Scanner(cssFile);
            while (fileReader.hasNextLine()) {

                String currentLine = fileReader.nextLine();

                // If we find variables, replace them, with their value
                if (currentLine.contains("var(")) {
                    for (CSSVar var : localCSSVars) {
                        while (currentLine.contains(var.getName())) {
                            currentLine = currentLine.replace("var(" + var.getName() + ")", var.getValue());
                        }
                    }
                }
                // Add new currentLine to be written
                filesNewInfo.add(currentLine);
            }
            fileReader.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Write the new files below
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(cssFile.getPath()));

            while (!filesNewInfo.isEmpty()) {
                writer.write(filesNewInfo.get(0));
                writer.newLine();
                filesNewInfo.remove(0);
            }
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:de.tudarmstadt.ukp.lmf.transform.sensealignments.FramenetWordnetAlignment.java

/**
 * Write output lines to given file/*from ww w .ja  v a  2 s  . c  om*/
 *
 * @param outFile
 * @param lines
 * @throws IOException
 */
protected static void writeLines(String outFile, Collection<String> lines) throws IOException {
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(new File(outFile)));
        for (String line : lines) {
            writer.write(line + "\n");
        }
    } catch (IOException e) {
        System.err.println("Exception" + e + "could not write to" + outFile);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:io.github.data4all.util.upload.GpxTrackUtil.java

/**
 * Method for reading a track from memory. Return a string representation of
 * a saved track.//from ww  w.jav  a2 s. c o m
 * 
 * @param context
 *            the context
 * @param trackXml
 *            the xml file of the gpx track
 * @param fileName
 *            the filename of the gpx track
 * @return a file object containing the gpx tracks
 */
private static File createGpxFile(Context context, String trackXml, String fileName) {
    final File file = new File(context.getFilesDir(), fileName);
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        writer.write(trackXml);
        writer.close();
    } catch (IOException e) {
        Log.e(GpxTrackUtil.class.getSimpleName(), "IOException: ", e);
    }
    return file;
}

From source file:com.packtpub.mahout.cookbook.chapter01.App.java

private static void CreateCsvRatingsFile() throws FileNotFoundException, IOException {

    BufferedReader br = new BufferedReader(new FileReader(inputFile));
    BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));

    String line = null;//from  w w w. j  a v  a 2 s . co m
    String line2write = null;
    String[] temp;

    int i = 0;
    while ((line = br.readLine()) != null && i < 1000) {
        i++;
        temp = line.split("::");
        line2write = temp[0] + "," + temp[1];
        bw.write(line2write);
        bw.newLine();
        bw.flush();

    }
    br.close();
    bw.close();
}