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:Main.java

public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file");
    if (!logFile.exists()) {
        try {/*from ww  w.j a  v a 2 s  .  co  m*/
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        BufferedReader buf = new BufferedReader(new FileReader(logFile));

        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        while ((line = buf.readLine()) != null) {
            lines.add(line);
        }

        int size = lines.size();

        //Every time the number of lines go over 1000, only add the last 500. This will keep its size to a minimum
        int i = lines.size() - 500;
        if (i < 0) {
            i = 0;
        }

        buf.close();

        BufferedWriter bufW = new BufferedWriter(new FileWriter(logFile, true));

        if (size > 1000) {
            bufW.write("");
            for (; i < lines.size(); i++) {
                bufW.append(line);
            }
        }

        bufW.append(text + "\n");
        bufW.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.cloudera.sqoop.manager.MySQLUtils.java

/**
 * Writes the user's password to a tmp file with 0600 permissions.
 * @return the filename used.//w w w  .  j av a 2 s. co m
 */
public static String writePasswordFile(Configuration conf) throws IOException {
    // Create the temp file to hold the user's password.
    String tmpDir = conf.get(ConfigurationConstants.PROP_JOB_LOCAL_DIRECTORY, "/tmp/");
    File tempFile = File.createTempFile("mysql-cnf", ".cnf", new File(tmpDir));

    // Make the password file only private readable.
    DirectImportUtils.setFilePermissions(tempFile, "0600");

    // If we're here, the password file is believed to be ours alone.  The
    // inability to set chmod 0600 inside Java is troublesome. We have to
    // trust that the external 'chmod' program in the path does the right
    // thing, and returns the correct exit status. But given our inability to
    // re-read the permissions associated with a file, we'll have to make do
    // with this.
    String password = conf.get(PASSWORD_KEY);
    BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile)));
    w.write("[client]\n");
    w.write("password=" + password + "\n");
    w.close();

    return tempFile.toString();
}

From source file:edu.isi.webserver.FileUtil.java

public static void writeStringToFile(String string, String fileName) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    writer.write(string);
    writer.flush();/* www  .  j a  va  2s .  c o  m*/
    writer.close();
}

From source file:com.pseudosudostudios.jdd.utils.ScoreSaves.java

private static void writeFile(Context c, List<Entry> entries) {
    Collections.sort(entries);//from w  w  w.j  a va2 s. com
    File file = new File(c.getFilesDir(), fileName);
    try {
        file.createNewFile();
        JSONArray array = new JSONArray();
        for (Entry e : entries)
            array.put(makeJSONObject(e));
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(array.toString());
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}

From source file:FileHandler.java

private static void writeToFile(String txt, FileContents fc) throws IOException {
    int sizeNeeded = txt.length() * 2;
    if (sizeNeeded > fc.getMaxLength()) {
        fc.setMaxLength(sizeNeeded);/*w w w.  ja va 2s.c o  m*/
    }
    BufferedWriter os = new BufferedWriter(new OutputStreamWriter(fc.getOutputStream(true)));
    os.write(txt);
    os.close();
}

From source file:Main.java

public static void write(File file, String data) {
    BufferedWriter out = null;
    try {/*from  w  w  w  . j  av  a2  s.c  o m*/
        out = new BufferedWriter(new FileWriter(file), 1024);
        out.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.lunix.cheata.utils.file.FileManager.java

public static void WriteFile(String fileName, String string) {
    try {/*from  w  w w.j  a  va2  s  .c o  m*/
        BufferedWriter writer = new BufferedWriter(
                new FileWriter(new File(getDir().getAbsolutePath(), fileName)));
        writer.write(string);
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.company.et.service.JsonService.java

/**
 *
 * @param json String what we write to file
 *//*from   ww  w.j a  v  a  2  s .c  o  m*/
public static void writeToFile(String json) {

    try {
        File file = new File(getFilename());
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.close();

        // @TODO: add LOG message
    } catch (IOException e) {
        e.printStackTrace();

        // @TODO: add LOG message
    }

}

From source file:com.joey.Fujikom.generate.Generate.java

/**
 * //ww  w  .ja  v  a  2s .  com
 * @param content
 * @param filePath
 */
public static void writeFile(String content, String filePath) {
    try {
        if (FileUtils.createFile(filePath)) {
            FileOutputStream fos = new FileOutputStream(filePath);
            Writer writer = new OutputStreamWriter(fos, "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(content);
            bufferedWriter.close();
            writer.close();
        } else {
            logger.info("??");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName,
        String pathSaveFile) {//from  w  w  w .  ja  v a 2  s .  co  m
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        // creating a new instance of a DOM to build a DOM tree.
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement(rootElement);
        // adding a node after the last child node of the specified node.
        doc.appendChild(root);

        Element interfaceElement = null;

        Element child = null;
        Text text;

        for (Object obj : listObject) {
            Class srcClass = obj.getClass();
            Field[] field = srcClass.getFields();
            interfaceElement = doc.createElement(interfaceName);
            for (int i = 0; i < field.length; i++) {
                // System.out.println(field[i].getName() + ":" +
                // field[i].get(obj));
                child = doc.createElement(field[i].getName());

                text = doc.createTextNode((field[i].get(obj)).toString());
                child.appendChild(text);

                interfaceElement.appendChild(child);
            }
            root.appendChild(interfaceElement);
        }

        // TransformerFactory instance is used to create Transformer
        // objects.
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        File file = new File(pathSaveFile);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write(xmlString);
        bw.flush();
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}