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:fll.web.WebTestUtils.java

/**
 * Submit a query to developer/QueryHandler, parse the JSON and return it.
 */// w ww. jav  a 2s .c  om
public static QueryHandler.ResultData executeServerQuery(final String query) throws IOException, SAXException {
    final WebClient conversation = getConversation();

    final URL url = new URL(TestUtils.URL_ROOT + "developer/QueryHandler");
    final WebRequest request = new WebRequest(url);
    request.setRequestParameters(
            Collections.singletonList(new NameValuePair(QueryHandler.QUERY_PARAMETER, query)));

    final Page response = loadPage(conversation, request);
    final String contentType = response.getWebResponse().getContentType();
    if (!"application/json".equals(contentType)) {
        final String text = getPageSource(response);
        final File output = File.createTempFile("json-error", ".html", new File("screenshots"));
        final FileWriter writer = new FileWriter(output);
        writer.write(text);
        writer.close();
        Assert.fail("Error JSON from QueryHandler: " + response.getUrl()
                + " Contents of error page written to: " + output.getAbsolutePath());
    }

    final String responseData = getPageSource(response);

    final ObjectMapper jsonMapper = new ObjectMapper();
    QueryHandler.ResultData result = jsonMapper.readValue(responseData, QueryHandler.ResultData.class);
    Assert.assertNull("SQL Error: " + result.getError(), result.getError());

    return result;
}

From source file:com.avatarproject.core.storage.UserCache.java

/**
 * Adds a player into the custom UserCache.
 * @param player Player to add to the cache.
 *//*w  w  w .  java2s.  c o  m*/
@SuppressWarnings("unchecked")
public static void addUser(Player player) {
    String name = player.getName();
    UUID uuid = player.getUniqueId();
    JSONArray array = getUserCache();
    try {
        for (int n = 0; n < array.size(); n++) { //Loop through all the objects in the array.
            JSONObject object = (JSONObject) array.get(n);
            if (object.get("id").equals(uuid.toString())) { //Check if the player's UUID exists in the cache.
                if (String.valueOf(object.get("name")).equalsIgnoreCase(name)) {
                    return;
                } else {
                    object.put("name", name); //Update the user.
                    FileWriter fileOut = new FileWriter(usercache);
                    fileOut.write(array.toJSONString()); //Write the JSON array to the file.
                    fileOut.close();
                    return;
                }
            }
        }
        JSONObject newEntry = new JSONObject();
        newEntry.put("id", uuid.toString());
        newEntry.put("name", name);
        array.add(newEntry); //Add a new player into the cache.
        FileWriter fileOut = new FileWriter(usercache);
        fileOut.write(array.toJSONString()); //Write the JSON array to the file.
        fileOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.mycompany.myelasticsearch.DocumentReader.java

public static void getAllCapitalWords(String docText) {
    Set<String> allCapsWords = new HashSet<>();
    Pattern p = Pattern.compile("\\b[A-Z]{2,}\\b");
    Matcher m = p.matcher(docText);
    while (m.find()) {
        String word = m.group();//from   w  ww  .jav a2s .  c o m
        // System.out.println(word);
        allCapsWords.add(word);
    }

    for (String allcaps : allCapsWords) {
        System.out.println(allcaps);
    }
    System.out.println("Caps word count" + allCapsWords.size());
    org.json.simple.JSONObject obj = new org.json.simple.JSONObject();
    int count = 0;
    for (String output : outputArray) {
        obj.put(String.valueOf(count), output.replaceAll("\\s+", " "));
        count++;
    }
    try {

        FileWriter file = new FileWriter("CapsWord.txt");
        file.write(obj.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.grillecube.engine.renderer.model.json.JSONHelper.java

public static void writeJSONObjectToFile(File file, JSONObject json) {
    try {// ww  w.j  av  a  2 s. com
        FileWriter writer = new FileWriter(file);
        writer.write(json.toString());
        writer.flush();
        writer.close();
    } catch (Exception e) {
        return;
    }
}

From source file:com.qwazr.utils.IOUtils.java

/**
 * Write the string to a file/*  w  w w  .j  a  va2 s.  c o  m*/
 *
 * @param content the text to write
 * @param file    the destination file
 * @throws IOException if any I/O error occured
 */
public static void writeStringAsFile(String content, File file) throws IOException {
    FileWriter writer = new FileWriter(file);
    try {
        writer.write(content);
    } finally {
        closeQuietly(writer);
    }
}

From source file:net.sourceforge.jcctray.utils.ObjectPersister.java

public static void saveCruiseRegistry(CruiseRegistry registry, FileWriter writer) throws IOException {
    try {/*from   ww w.  ja  v  a  2s . c o  m*/
        writer.write("<?xml version='1.0' ?>\n");
        writer.write("<cruiseregistry>\n");
        writer.write("   <cruiseImpls>\n");
        saveCruiseImpls(writer, registry.getCruiseImpls());
        writer.write("   </cruiseImpls>\n");
        writer.write("</cruiseregistry>\n");
    } catch (IOException e) {
        throw e;
    } finally {
        writer.close();
    }
}

From source file:ai.susi.json.JsonFile.java

/**
 * write a json file in transaction style: first write a temporary file,
 * then rename the original file to another temporary file, then rename the
 * just written file to the target file name, then delete all other temporary files.
 * @param file/* w  ww  .j av  a 2 s .c om*/
 * @param json
 * @throws IOException
 */
public static void writeJson(File file, JSONObject json) throws IOException {
    if (file == null)
        throw new IOException("file must not be null");
    if (json == null)
        throw new IOException("json must not be null");
    if (!file.exists())
        file.createNewFile();
    File tmpFile0 = new File(file.getParentFile(), file.getName() + "." + System.currentTimeMillis());
    File tmpFile1 = new File(tmpFile0.getParentFile(), tmpFile0.getName() + "1");
    FileWriter writer = new FileWriter(tmpFile0);
    writer.write(json.toString(2));
    writer.close();
    file.renameTo(tmpFile1);
    tmpFile0.renameTo(file);
    tmpFile1.delete();
}

From source file:org.web4thejob.util.L10nUtil.java

public static void logMissingMessage(String code, String defaultValue) {
    File file = new File(System.getProperty("user.home"),
            "w4tj_" + CoreUtil.getUserLocale().toString() + ".log");
    FileWriter out;
    try {/*  w  w w . j ava2  s  .  c  o m*/
        out = new FileWriter(file, true);
        out.write(code + "=" + defaultValue + System.getProperty("line.separator"));
        out.close();
        // logger.warn("Missiing message: " + code + "=" + defaultValue);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.amazonaws.mturk.cmd.MakeTemplate.java

private static void generateScript(String scriptTemplateDir, String target, String targetDirPath,
        String command, CreateScriptUtil.ScriptType type) throws Exception {

    String scriptName = targetDirPath + File.separator + command + type.getExtension();

    System.out.println("Generating script: " + scriptName);

    Map<String, String> input = new HashMap<String, String>(1);
    input.put("${target}", target);

    String templateFileName = scriptTemplateDir + File.separator + command + "." + type + ".template";
    String source = CreateScriptUtil.generateScriptSource(type, input, templateFileName.toString());

    FileWriter out = new FileWriter(scriptName);
    out.write(source);
    out.close();//from  w  w  w .j a va  2s  .  c  o  m
}

From source file:org.openmrs.module.pcslabinterface.PcsLabInterfaceUtil.java

public static void stringToFile(String fileContents, File outFile) throws IOException {
    FileWriter writer = new FileWriter(outFile);

    writer.write(fileContents);

    writer.close();/*from   w  w w  . jav a 2  s .c o m*/
}