Example usage for java.io FileWriter write

List of usage examples for java.io FileWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:Main.java

public static boolean writeOneLine(String filename, String value) {
    FileWriter fileWriter = null;
    try {// w ww .ja v  a2  s.  co m
        fileWriter = new FileWriter(filename);
        fileWriter.write(value);
    } catch (IOException e) {
        String Error = "Error writing { " + value + " } to file: " + filename;
        Log.e(TAG, Error, e);
        return false;
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException ignored) {
                // failed to close writer
            }
        }
    }
    return true;
}

From source file:fm.moe.android.util.JSONFileHelper.java

public static void write(final JSONObject object, final String path) throws IOException {
    if (object == null || path == null)
        return;//from  ww  w .  j  a  v  a  2s .c  o  m
    new File(path).delete();
    final RandomAccessFile raf = new RandomAccessFile(path, FILE_MODE_RW);
    final FileWriter fw = new FileWriter(raf.getFD());
    fw.write(object.toString());
    fw.flush();
    fw.close();
}

From source file:Main.java

/**
 * Write the String into the given File/*  w  w  w  .java 2  s .c om*/
 * @param baseString
 * @param mainStyleFile
 */
public static void writeStyleFile(String baseString, File mainStyleFile) {
    FileWriter fw = null;
    try {
        fw = new FileWriter(mainStyleFile);
        fw.write(baseString);
        Log.i("STYLE", "Style File updated:" + mainStyleFile.getPath());
    } catch (FileNotFoundException e) {
        Log.e("STYLE", "unable to write open file:" + mainStyleFile.getPath());
    } catch (IOException e) {
        Log.e("STYLE", "error writing the file: " + mainStyleFile.getPath());
    } finally {
        try {
            if (fw != null) {
                fw.close();
            }
        } catch (IOException e) {
            // ignored
        }
    }
}

From source file:Main.java

public static void saveFile(String tPath, String name, String text) {
    try {/*  w ww .j  av  a  2 s.  co m*/
        File file = new File(tPath);
        file.mkdirs();
        FileWriter fw = new FileWriter(tPath + name, false);
        fw.write(text);
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.daphne.es.maintain.icon.web.controller.tmp.GenCssSql.java

private static void readUploadFile() throws IOException {
    String fromFile = "E:\\workspace\\git\\es\\web\\src\\main\\webapp\\WEB-INF\\static\\comp\\zTree\\css\\zTreeStyle\\img\\diy\\icon.txt";
    String toFile = "C:\\Documents and Settings\\Administrator\\?\\b.sql";
    String template = "insert into `maintain_icon` (`id`, `identity`, `img_src`, `type`, `width`, `height`) values(%1$d, '%2$s', '%3$s', 'upload_file', %4$d, %5$d);";

    List<String> list = FileUtils.readLines(new File(fromFile));
    FileWriter writer = new FileWriter(toFile);

    int count = 300;
    for (int i = 0, l = list.size(); i < l; i += 2) {
        writer.write(String.format(template, count++, list.get(i), list.get(i + 1), 16, 16));
        writer.write("\r\n");
    }//w w w  .  j  a  va  2 s.c om

    writer.close();
}

From source file:net.chunkyhosting.Roe.CHGManagerLauncher.utils.JSON.java

public static void writeJsonToFile(JSONObject json, File file) throws IOException {

    FileWriter writer = new FileWriter(file.toString());

    try {/* w w  w. j  av a  2s .c o  m*/

        writer.write(json.toString(4));

    } finally {

        writer.flush();
        writer.close();

    }

}

From source file:Main.java

public static void delete(String filename, int startline, int numlines) {
    try {// ww w .j av a 2s .  c  o  m
        BufferedReader br = new BufferedReader(new FileReader(filename));

        //String buffer to store contents of the file
        StringBuffer sb = new StringBuffer("");

        //Keep track of the line number
        int linenumber = 1;
        String line;

        while ((line = br.readLine()) != null) {
            //Store each valid line in the string buffer
            if (linenumber < startline || linenumber >= startline + numlines)
                sb.append(line + "\n");
            linenumber++;

        }

        if (startline + numlines > linenumber)
            System.out.println("End of file reached.");
        br.close();

        FileWriter fw = new FileWriter(new File(filename));
        //Write entire string buffer into the file
        fw.write(sb.toString());
        fw.close();
    } catch (Exception e) {
        System.out.println("Something went horribly wrong: " + e.getMessage());
    }
}

From source file:Main.java

public static void writeToFile(Iterator<?> iter, File file) throws IOException {
    if (iter != null && file != null) {
        file.getParentFile().mkdirs();//from w w  w .  jav a2s .  com
        File tempFile = new File(file.getAbsolutePath() + ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        for (; iter.hasNext();) {
            fw.write(new StringBuilder(iter.next().toString()).append("\r\n").toString());
        }
        fw.flush();
        fw.close();
        file.delete();
        tempFile.renameTo(file);
    }
}

From source file:TestMapObjInterface.java

public static boolean runTest(String file, String fsName, String cnaddress) {
    try {/*from  w  w w  .  j  av  a 2  s .  c  o m*/
        long targetNumChunks = 2;
        FileWriter godot = new FileWriter(file);
        File monet = new File(file);
        godot.write("I'm waiting.");
        godot.flush();
        while ((monet.length() / CHUNK_SIZE + 1) < targetNumChunks) {
            godot.write("I'm waiting.");
        }
        godot.flush();
        godot.close();
        ArrayList<String> chunks = getChunks(file, fsName, cnaddress);
        long fileChunks = (monet.length() / CHUNK_SIZE) + 1;
        if (fileChunks == chunks.size()) {
            return true;
        } else {
            System.out.print("expected " + fileChunks + " chunks, got " + chunks.size() + ": ");
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static void writeToFile(Collection<?> collection, File file) throws IOException {
    if (collection != null && file != null) {
        file.getParentFile().mkdirs();//from w w w .ja  va  2  s  .  c  o  m
        File tempFile = new File(file.getAbsolutePath() + ".tmp");
        FileWriter fw = new FileWriter(tempFile);
        for (Object obj : collection) {
            fw.write(new StringBuilder(obj.toString()).append("\r\n").toString());
        }
        fw.flush();
        fw.close();
        file.delete();
        tempFile.renameTo(file);
    }
}