Example usage for java.io FileWriter flush

List of usage examples for java.io FileWriter flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:Main.java

public static void write(String in, File file, boolean append) {
    if (file.exists()) {
        file.delete();/*from  ww w. j a v a2s .c om*/
    }
    try {
        file.createNewFile();
        FileWriter fw = new FileWriter(file, append);
        fw.write(in);
        fw.flush();
        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;//from w w w  .  j av 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:cfd.backupper.state.StartupConfig.java

public static void putSetting(String key, List l) {

    //l needs to be toString(), otherwise there are no doublequotes in JSON.
    List stringedList = (List) l.stream().map(elem -> elem.toString()).collect(Collectors.toList());
    if (jo.containsKey(key)) {
        jo.replace(key, stringedList);/*ww w . j  av  a  2 s . c  o m*/
    } else {
        jo.put(key, stringedList);
    }
    try {
        FileWriter fw = new FileWriter(confFile, false);
        fw.append(jo.toJSONString());
        fw.flush();
        fw.close();
    } catch (IOException ex) {
        Logger.getLogger(StartupConfig.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Main.java

/**
 * write a string To a File/*w w  w  . j  a v  a  2  s.c  o  m*/
 * 
 * @param context
 * @param file
 * @param string
 * @param isAppend
 * @return
 */
public static boolean writeStringToFile(File file, String string, boolean isAppend) {
    boolean isWriteOk = false;

    if (null == file || null == string) {
        return isWriteOk;
    }

    FileWriter fw = null;
    try {
        fw = new FileWriter(file, isAppend);

        fw.write(string, 0, string.length());
        fw.flush();
        isWriteOk = true;
    } catch (Exception e) {
        isWriteOk = false;
        e.printStackTrace();
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
                isWriteOk = false;
                e.printStackTrace();
            }
        }
    }
    return isWriteOk;
}

From source file:TestMapObjInterface.java

public static boolean runTest(String file, String fsName, String cnaddress) {
    try {//from  w w w  . j  a  v a  2s . 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:net.firejack.platform.core.utils.MiscUtils.java

/**
 * @param properties//from  www  .j  ava  2  s  . co  m
 * @param checkFileExists
 * @param property
 * @param value
 * @throws java.io.IOException
 */
public static void setProperties(File properties, boolean checkFileExists, String property, String value)
        throws IOException {
    if (checkFileExists && !properties.exists()) {
        logger.error("Properties file [" + properties.getAbsolutePath() + "] does not exist.");
        throw new FileNotFoundException("Properties file does not found.");
        //            IOHelper.delete(dbProperties);
    }
    Properties props = new Properties();
    if (properties.exists()) {
        FileReader reader = new FileReader(properties);
        props.load(reader);
        reader.close();
    }
    props.put(property, value);
    FileWriter writer = new FileWriter(properties);
    props.store(writer, null);
    writer.flush();
    writer.close();
}

From source file:net.firejack.platform.core.utils.MiscUtils.java

/**
 * @param properties// w ww. ja v a  2s  . co m
 * @param append
 * @throws java.io.IOException
 */
public static void setProperties(File properties, Map<String, String> append) throws IOException {
    Properties props = new Properties();
    if (properties.exists()) {
        FileReader reader = new FileReader(properties);
        props.load(reader);
        reader.close();
    } else {
        FileUtils.forceMkdir(properties.getParentFile());
    }

    if (properties.exists() || properties.createNewFile()) {
        props.putAll(append);
        FileWriter writer = new FileWriter(properties);
        props.store(writer, null);
        writer.flush();
        writer.close();
    }
}

From source file:ca.uvic.cs.tagsea.wizards.TagNetworkSender.java

public static synchronized void send(String xml, IProgressMonitor sendMonitor, int id)
        throws InvocationTargetException {
    sendMonitor.beginTask("Uploading Tags", 100);
    sendMonitor.subTask("Saving Temporary file...");
    File location = TagSEAPlugin.getDefault().getStateLocation().toFile();
    File temp = new File(location, "tagsea.temp.file.txt");
    if (temp.exists()) {
        String message = "Unable to send tags. Unable to create temporary file.";
        IOException ex = new IOException(message);
        throw (new InvocationTargetException(ex, message));
    }/*from   w w  w  .  jav  a2  s  . c o m*/
    try {
        FileWriter writer = new FileWriter(temp);
        writer.write(xml);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new InvocationTargetException(e);
    }
    sendMonitor.worked(5);
    sendMonitor.subTask("Uploading Tags...");
    String uploadScript = "http://stgild.cs.uvic.ca/cgi-bin/tagSEAUpload.cgi";
    PostMethod post = new PostMethod(uploadScript);

    String fileName = getToday() + ".txt";
    try {
        Part[] parts = { new StringPart("KIND", "tag"), new FilePart("MYLAR" + id, fileName, temp) };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        HttpClient client = new HttpClient();
        int status = client.executeMethod(post);
        String resp = getData(post.getResponseBodyAsStream());
        if (status != 200) {
            IOException ex = new IOException(resp);
            throw (ex);
        }
    } catch (IOException e) {
        throw new InvocationTargetException(e, e.getLocalizedMessage());
    } finally {
        sendMonitor.worked(90);
        sendMonitor.subTask("Deleting Temporary File");
        temp.delete();
        sendMonitor.done();
    }

}

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 a v  a2  s .c o m

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

    } finally {

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

    }

}

From source file:com.ratebeer.android.gui.components.helpers.ImportExport.java

/**
 * Export the offline ratings from the database to a file
 * @param list The offline ratings to export
 * @param outputfile The file to write to
 * @throws JSONException Thrown when the content could not be written to JSON
 * @throws IOException Thrown when the file could not be written to
 *//*w ww.  ja va2 s .c  o m*/
public static void exportRatings(List<OfflineRating> list, File outputfile) throws JSONException, IOException {

    // Create a single JSON object with all offline ratings
    JSONObject json = new JSONObject();
    JSONArray ratings = new JSONArray();

    for (OfflineRating item : list) {
        JSONObject rating = new JSONObject();
        rating.put("beerId", item.getBeerId());
        rating.put("beerName", item.getBeerName());
        rating.put("originalRatingId", item.getOriginalRatingId());
        rating.put("originalRatingDate", item.getOriginalRatingDate());
        rating.put("aroma", item.getAroma());
        rating.put("appearance", item.getAppearance());
        rating.put("taste", item.getTaste());
        rating.put("palate", item.getPalate());
        rating.put("overall", item.getOverall());
        rating.put("comments", item.getComments());
        rating.put("timeSaved", item.getTimeSaved().getTime());
        ratings.put(rating);
    }
    json.put("ratings", ratings);

    // Serialize the JSON object to a file
    if (outputfile.exists()) {
        outputfile.delete();
    }
    outputfile.getParentFile().mkdirs();
    outputfile.createNewFile();
    FileWriter writer = new FileWriter(outputfile);
    writer.write(json.toString(2));
    writer.flush();
    writer.close();

}