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:model.settings.ReadSettings.java

/**
 * reads text from a configuration file; if is configuration file 
 * is not valid, return null; otherwise return configuration line.
 * /*  w  ww  . ja v  a  2  s . c  o m*/
 * @param _path from which _path is red.
 * @return the text read form file @ _path.
 * 
 * @throws IOException is thrown in case of error.
 */
public static void changeOption(final String _operation, final String _newValue) throws IOException {

    //create Reader
    FileReader fr = new FileReader(PROGRAM_SETTINGS_LOCATION);
    BufferedReader br = new BufferedReader(fr);

    String sumLine = "";
    String currentLine = "";
    boolean found = false;
    currentLine = br.readLine();
    while (currentLine != null) {

        if (!found) {
            sumLine += currentLine + "\n";
        } else {
            found = !found;
            sumLine += _newValue + "\n";
        }

        // if the current line is the identifier of the current
        // operation that has to be changed.
        if (currentLine != null && currentLine.equals(_operation)) {
            found = true;
        }
        currentLine = br.readLine();

    }

    //close reader
    br.close();
    fr.close();

    FileWriter fw = new FileWriter(PROGRAM_SETTINGS_LOCATION);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(sumLine);
    bw.flush();
    bw.close();
    fw.close();
}

From source file:net.blogracy.controller.ActivitiesController.java

public String seedActivityStream(String userId, final List<ActivityEntry> feed)
        throws JSONException, IOException {
    final File feedFile = new File(CACHE_FOLDER + File.separator + userId + ".json");

    JSONArray items = new JSONArray();
    for (int i = 0; i < feed.size(); ++i) {
        JSONObject item = new JSONObject(feed.get(i));
        items.put(item);//from   w  w  w  . j  a v  a  2s. co m
    }
    JSONObject db = new JSONObject();

    db.put("items", items);

    FileWriter writer = new FileWriter(feedFile);
    db.write(writer);
    writer.close();

    String feedUri = sharing.seed(feedFile);
    return feedUri;
}

From source file:SiteFrame.java

public void saveContent() {
    try {/*from   ww w. j a  va 2 s . c  o  m*/
        FileWriter fw = new FileWriter(filename);
        ta.write(fw);
        fw.close();
    } catch (Exception e) {
        System.err.println("Could not save page: " + filename);
    }
}

From source file:com.spider.ssh.SshConfigFile.java

/**
 * Saves Host objects to disk at the original location.
 * @throws IOException//from w  w w  .  ja  va  2 s .  co  m
 */
public void save() throws IOException {
    FileWriter writer = new FileWriter(source);

    save(writer);

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

From source file:com.bt.aloha.testing.SimpleSipStackLogEnhancerTest.java

private void writeSourceFile(String text) throws IOException {
    FileWriter fw = null;
    try {/* www  .ja va2s  . c om*/
        fw = new FileWriter(tempSourceFile);
        fw.write(text);
    } finally {
        if (fw != null)
            fw.close();
    }
}

From source file:de.hstsoft.sdeep.NoteManager.java

@SuppressWarnings("unchecked")
private void saveNotes() {
    JSONObject envelope = new JSONObject();
    envelope.put("version", VERSION);

    JSONArray jsonArray = new JSONArray();
    for (Note n : notes) {
        jsonArray.add(n.toJson());//www . j  a  v a  2 s.co  m
    }

    envelope.put("notes", jsonArray);

    try {
        File file = new File(directory + FILENAME);
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.append(envelope.toJSONString());
        fileWriter.flush();
        fileWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.juick.android.Utils.java

public static void setupWebView(final WebView wv, String content) {
    try {/*from   w  ww.ja v  a  2  s.c om*/
        if (skipWeb)
            return;
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(wv.getContext());
        File file = new File(wv.getContext().getCacheDir(), "temp.html");
        String PREFIX = "#prefs.checked.";
        while (true) {
            int ix = content.indexOf(PREFIX);
            if (ix == -1)
                break;
            int ix2 = content.indexOf("#", ix + 1);
            if (ix2 == -1)
                break;
            String key = content.substring(ix + PREFIX.length(), ix2);
            boolean def = false;
            if (key.endsWith("!")) {
                def = true;
                key = key.substring(0, key.length() - 1);
            }
            boolean checked = sp.getBoolean(key, def);
            content = content.substring(0, ix) + (checked ? "checked" : "") + content.substring(ix2 + 1);
        }
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(content);
        fileWriter.close();
        wv.getSettings().setJavaScriptEnabled(true);
        //            wv.getSettings().setBlockNetworkImage(false);
        //            wv.getSettings().setBlockNetworkLoads(false);
        wv.getSettings().setLoadsImagesAutomatically(true);
        Uri uri = Uri.fromFile(file);
        wv.addJavascriptInterface(new Object() {
            public void onFormData(String str) {
                wv.setTag(str);
            }
        }, "EXT");
        wv.loadUrl(uri.toString());
    } catch (IOException e) {
        //
    }

}

From source file:com.brienwheeler.svc.content.impl.ContentServiceTest.java

@Test(expected = ContentServiceConfigException.class)
public void testSetBaseDirectoryFailNotDir() throws IOException {
    try {// w  ww .  j ava2 s.  c om
        FileWriter writer = new FileWriter(baseDirectory);
        writer.write("foo\n");
        writer.close();

        contentService.setBaseDirectory(baseDirectory);
        Assert.fail();
    } catch (ContentServiceConfigException e) {
        baseDirectory.delete();
        throw e;
    }
}

From source file:net.blogracy.controller.ActivitiesController.java

public void addFeedEntry(String id, String text, File attachment) {
    try {/* ww  w .  j a  v  a2 s . c  om*/
        String hash = sharing.hash(text);
        File textFile = new File(CACHE_FOLDER + File.separator + hash + ".txt");

        FileWriter w = new FileWriter(textFile);
        w.write(text);
        w.close();

        String textUri = sharing.seed(textFile);
        String attachmentUri = null;
        if (attachment != null) {
            attachmentUri = sharing.seed(attachment);
        }

        final List<ActivityEntry> feed = getFeed(id);
        final ActivityEntry entry = new ActivityEntryImpl();
        entry.setVerb("post");
        entry.setUrl(textUri);
        entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
        entry.setContent(text);
        if (attachment != null) {
            ActivityObject enclosure = new ActivityObjectImpl();
            enclosure.setUrl(attachmentUri);
            entry.setObject(enclosure);
        }
        feed.add(0, entry);
        String feedUri = seedActivityStream(id, feed);
        DistributedHashTable.getSingleton().store(id, feedUri, entry.getPublished());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.googlecode.fascinator.portal.process.RecordProcessor.java

/**
 * 'post' - processing method.//from   w w  w.  j  a v  a2 s .  c om
 * 
 * he 'inputKey' entry in the dataMap contains ids queued for resending.
 * This is merged with the 'includeList' and persisted on the config file.
 * The 'lastRun' is updated and persisted as well.
 * 
 * @param id
 * @param inputKey
 * @param configFilePath
 * @param dataMap
 * @return
 * @throws Exception
 */
private boolean postProcess(String id, String inputKey, String configFilePath, HashMap<String, Object> dataMap)
        throws Exception {
    File configFile = new File(configFilePath);
    SimpleDateFormat dtFormat = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss'Z'");
    JsonSimple config = new JsonSimple(configFile);
    config.getJsonObject().put("lastrun", dtFormat.format(new Date()));
    Collection<String> oids = (Collection<String>) dataMap.get(inputKey);
    JSONArray includedArr = config.getArray("includeList");
    if (oids != null && oids.size() > 0) {
        // some oids failed, writing it to inclusion list so it can be sent
        // next time...
        if (includedArr == null) {
            includedArr = config.writeArray("includeList");
        }
        includedArr.clear();
        for (String oid : oids) {
            includedArr.add(oid);
        }
    } else {
        // no oids failed, all good, clearing the list...
        if (includedArr != null && includedArr.size() > 0) {
            includedArr.clear();
        }
    }
    FileWriter writer = new FileWriter(configFile);
    writer.write(config.toString(true));
    writer.close();
    return true;
}