Example usage for java.io FileWriter close

List of usage examples for java.io FileWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Main.java

public static void writeJsonOnDisk(Context context, String fileName, StringBuilder bigStr) {
    try {/*from w  w  w. j  av a  2s  .c o m*/
        FileWriter Filewriter = new FileWriter(context.getApplicationInfo().dataDir + "/" + fileName + ".json");
        Filewriter.write(bigStr.toString());
        Filewriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void write(String in, File file, boolean append) {
    if (file.exists()) {
        file.delete();/*from   w ww .j a v a2 s  .  com*/
    }
    try {
        file.createNewFile();
        FileWriter fw = new FileWriter(file, append);
        fw.write(in);
        fw.flush();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:JSONUtil.java

/**
 * @param out/*from w  ww. j a  va2 s  .  c o m*/
 * @param string
 * @throws IOException
 * @throws JSONException
 */
public static void saveJson(JSONObject out, String file) throws IOException, JSONException {
    File fo = new File(file);
    FileWriter fw = new FileWriter(fo);
    fw.append(out.toString(4));
    fw.close();
}

From source file:Main.java

private static void writeFile(String filePath, String content) throws IOException {
    FileWriter fw = new FileWriter(filePath);
    PrintWriter out = new PrintWriter(fw);
    out.write(content);// w w  w. j  av  a  2s . com
    out.println();
    fw.close();
    out.close();
}

From source file:Main.java

public static void createNewResultFileForConnexion() {
    Date currentDate = new Date();
    SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyyMMdd");
    String logPath = LOG_FOLDER + "result_" + localDateFormat.format(currentDate) + "_"
            + System.currentTimeMillis() + ".txt";

    try {/*  www.  j  a v  a  2  s  . c o m*/
        File logFile = new File(logPath);
        FileWriter writer = new FileWriter(logFile, Boolean.TRUE);
        writer.write("Command\tCommand(hex)\tResult\tResult(Hex)\tLatency\n");
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Writes all characters from a <tt>Reader</tt> to a file using the default
 * character encoding.//from   w  w w. j ava 2s.c om
 *
 * @param reader The <tt>Reader</tt> containing the data to write to the
 * file.
 * @param file The file to write the data to.
 * @return The total number of characters written.
 * @throws IOException If an I/O error occured while trying to write the
 * data to the file.
 * @see java.io.FileWriter
 */
public static final long writeToFile(Reader reader, File file) throws IOException {
    FileWriter writer = new FileWriter(file);

    try {
        return transfer(reader, writer);
    } finally {
        writer.close();
    }
}

From source file:com.bluexml.side.build.tools.graph.JungConverter.java

public static void saveGraph(Graph<Componant, String> g, File file) throws IOException {
    logger.debug("Save Graph");
    GraphMLWriter<Componant, String> gw = new GraphMLWriter<Componant, String>();
    gw.setEdgeIDs(new Transformer<String, String>() {

        public String transform(String input) {
            // TODO Auto-generated method stub
            return input;
        }//  w ww . j a  v a  2 s.  c om
    });
    FileWriter w = new FileWriter(file);
    gw.save(g, w);
    w.close();

    printGraphStats(g, "graph -> file");
}

From source file:Main.java

public static void saveFile(String tPath, String name, String text) {
    try {/*from  ww w . j ava  2s . 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:fm.moe.android.util.JSONFileHelper.java

public static void write(final JSONObject object, final String path) throws IOException {
    if (object == null || path == null)
        return;// w ww. j  a  v  a  2 s.  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

public static void delete(String filename, int startline, int numlines) {
    try {/*from  w ww .  java2  s  . c  om*/
        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());
    }
}