List of usage examples for java.io FileWriter close
public void close() throws IOException
From source file:Main.java
/** * write a string To a File// w w w . j a v a 2 s. com * * @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:Main.java
public static boolean writeOneLine(String filename, String value) { FileWriter fileWriter = null; try {//from ww w. j av a 2 s.c o 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:com.ccoe.build.core.utils.FileUtils.java
public static void writeToFile(File targetFile, String body) { FileWriter fileWriter = null; try {/*from www .j ava 2s. c om*/ fileWriter = new FileWriter(targetFile); fileWriter.write(body); } catch (IOException ex) { ex.printStackTrace(); } finally { try { fileWriter.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:io.github.seanboyy.lotReset.json.ReadJSON.java
/**Read JSON file by using configuration file's JSON value, * which points to the file/*from w w w .ja v a 2 s . c om*/ * @param configLocation String which specifies the location of the config.properties file * @return <code>ArrayList<ArrayList<Lot>> lots</code> if no errors are encountered * @since 1.0 - Implemented config.properties in 2.0 */ public static ArrayList<ArrayList<Lot>> read(String configLocation) { JSONParser parser = new JSONParser(); URL url; File f; //lots is multidimensional in this format: {{fromLot,toLot},{fromLot,toLot},{fromLot,toLot},etc...} ArrayList<ArrayList<Lot>> lots = new ArrayList<ArrayList<Lot>>(); Config config = ReadConfig.read(configLocation); final String[] alphabet = config.getAlpha(); final String[] types = config.getType(); final String[] worlds = config.getWorld(); try { url = new URL(config.getJSON()); InputStream in = url.openStream(); f = File.createTempFile("temp", "json"); FileWriter file = new FileWriter(f); Scanner input = new Scanner(in); while (input.hasNextLine()) { file.write(input.nextLine()); } input.close(); file.close(); Object obj = parser.parse(new FileReader(f)); JSONObject jsonObj = (JSONObject) obj; JSONObject regions = (JSONObject) jsonObj.get("Regions"); for (String a : worlds) { for (String b : types) { for (String c : alphabet) { for (int d = 1; d <= alphabet.length; ++d) { String lotId = a + "-" + b + "-" + c + d; String lotIdA = a + "_" + b + "-" + c + d; String lotIdB = a + "-" + b + "_" + c + d; String lotIdC = a + "_" + b + "_" + c + d; JSONObject lot = (JSONObject) regions.get(lotId); JSONObject lotA = (JSONObject) regions.get(lotIdA); JSONObject lotB = (JSONObject) regions.get(lotIdB); JSONObject lotC = (JSONObject) regions.get(lotIdC); ArrayList<Lot> lotInfo = new ArrayList<Lot>(); if (lot != null) { lotInfo.add(new Lot((long) lot.get("source_minX"), (long) lot.get("source_maxX"), (long) lot.get("source_minY"), (long) lot.get("source_maxY"), (long) lot.get("source_minZ"), (long) lot.get("source_maxZ"), (String) lot.get("source_file"), lotId)); lotInfo.add(new Lot((long) lot.get("dest_minX"), (long) lot.get("dest_maxX"), (long) lot.get("dest_minY"), (long) lot.get("dest_maxY"), (long) lot.get("dest_minZ"), (long) lot.get("dest_maxZ"), (String) lot.get("dest_file"), lotId)); lots.add(lotInfo); } if (lotA != null) { lotInfo.add(new Lot((long) lotA.get("source_minX"), (long) lotA.get("source_maxX"), (long) lotA.get("source_minY"), (long) lotA.get("source_maxY"), (long) lotA.get("source_minZ"), (long) lotA.get("source_maxZ"), (String) lotA.get("source_file"), lotIdA)); lotInfo.add(new Lot((long) lotA.get("dest_minX"), (long) lotA.get("dest_maxX"), (long) lotA.get("dest_minY"), (long) lotA.get("dest_maxY"), (long) lotA.get("dest_minZ"), (long) lotA.get("dest_maxZ"), (String) lotA.get("dest_file"), lotIdA)); lots.add(lotInfo); } if (lotB != null) { lotInfo.add(new Lot((long) lotB.get("source_minX"), (long) lotB.get("source_maxX"), (long) lotB.get("source_minY"), (long) lotB.get("source_maxY"), (long) lotB.get("source_minZ"), (long) lotB.get("source_maxZ"), (String) lotB.get("source_file"), lotIdB)); lotInfo.add(new Lot((long) lotB.get("dest_minX"), (long) lotB.get("dest_maxX"), (long) lotB.get("dest_minY"), (long) lotB.get("dest_maxY"), (long) lotB.get("dest_minZ"), (long) lotB.get("dest_maxZ"), (String) lotB.get("dest_file"), lotIdB)); lots.add(lotInfo); } if (lotC != null) { lotInfo.add(new Lot((long) lotC.get("source_minX"), (long) lotC.get("source_maxX"), (long) lotC.get("source_minY"), (long) lotC.get("source_maxY"), (long) lotC.get("source_minZ"), (long) lotC.get("source_maxZ"), (String) lotC.get("source_file"), lotIdC)); lotInfo.add(new Lot((long) lotC.get("dest_minX"), (long) lotC.get("dest_maxX"), (long) lotC.get("dest_minY"), (long) lotC.get("dest_maxY"), (long) lotC.get("dest_minZ"), (long) lotC.get("dest_maxZ"), (String) lotC.get("dest_file"), lotIdC)); lots.add(lotInfo); } } } } } } catch (IOException e) { System.out.println("FILE ERROR FROM READING JSON"); e.printStackTrace(); return null; } catch (ParseException e) { System.out.println("PARSER ERROR FROM READING JSON"); e.printStackTrace(); return null; } return lots; }
From source file:net.portalblock.untamedchat.bungee.UCConfig.java
public static void load() { final String NEW_LINE = System.getProperty("line.separator"); File cfgDir = new File("plugins/UntamedChat"); if (!cfgDir.exists()) { UntamedChat.getInstance().getLogger().info("No config directory found, generating one now!"); cfgDir.mkdir();//from ww w . j a v a2s. c o m } File configFile = new File(cfgDir + "/config.json"); if (!configFile.exists()) { UntamedChat.getInstance().getLogger().info("No config file found, generating one now!"); try { configFile.createNewFile(); InputStream is = UCConfig.class.getResourceAsStream("/config.json"); String line; if (is == null) throw new NullPointerException("is"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); FileWriter configWriter = new FileWriter(configFile); while ((line = reader.readLine()) != null) { configWriter.write(line + NEW_LINE); } configWriter.flush(); configWriter.close(); reader.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } try { BufferedReader reader = new BufferedReader(new FileReader(configFile)); StringBuilder configBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { configBuilder.append(line); } JSONObject config = new JSONObject(configBuilder.toString()); TARGET_FORMAT = config.optString("target_format", "&6{sender} &7-> &6Me&7: {msg}"); SENDER_FORMAT = config.optString("sender_format", "&6Me &7-> &6{target}&7: {msg}"); SOCIAL_SPY_FORMAT = config.optString("social_spy_format", "{sender} -> {target}: {msg}"); GLOBAL_FORMAT = config.optString("global_format", "&7[&6{server}&7] [&6{sender}&7]: &r{msg}"); gcDefault = config.optBoolean("global_chat_default", false); chatCoolDowns = config.optBoolean("enable_chat_cooldown", true); spDefault = config.optBoolean("social_spy_default", false); chatCooldown = config.optLong("chat_cooldown", 10); SPAM_MESSAGE = config.optString("spam_message", "&7[&cUntamedChat&7] &cDon't spam the chat!"); JSONObject commands = config.optJSONObject("commands"); if (commands == null) { msgAliases = new String[] { "msg", "m" }; replyAliases = new String[] { "reply", "r" }; globalAliases = new String[] { "globalchat", "global", "g" }; socialSpyAliases = new String[] { "socialspy", "sp", "spy" }; } else { msgAliases = makeCommandArray(commands.optJSONArray("msg"), "msg", "m"); replyAliases = makeCommandArray(commands.optJSONArray("reply"), "reply", "r"); globalAliases = makeCommandArray(commands.optJSONArray("global_chat"), "globalchat", "global", "g"); socialSpyAliases = makeCommandArray(commands.optJSONArray("social_spy"), "socialspy", "sp", "spy"); } } catch (IOException e) { e.printStackTrace(); } }
From source file:apm.generate.Generate.java
/** * /*from ww w . jav a 2 s.c o m*/ * @param content * @param filePath */ public static void writeFile(String content, String filePath) { try { if (FileUtils.createFile(filePath)) { FileWriter fileWriter = new FileWriter(filePath, true); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(content); bufferedWriter.close(); fileWriter.close(); } else { //logger.info("??"); System.out.println("??"); } } catch (Exception e) { e.printStackTrace(); } }
From source file:au.org.ala.spatial.util.UploadSpatialResource.java
public static String loadCreateStyle(String url, String extra, String username, String password, String name) { System.out.println("loadCreateStyle url:" + url); System.out.println("name:" + name); String output = ""; HttpClient client = new HttpClient(); client.setConnectionTimeout(10000);/*from w w w . j a v a 2 s . c o m*/ client.setTimeout(60000); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); PostMethod post = new PostMethod(url); post.setDoAuthentication(true); // Execute the request try { // Request content will be retrieved directly // from the input stream File file = File.createTempFile("sld", "xml"); FileWriter fw = new FileWriter(file); fw.append("<style><name>" + name + "</name><filename>" + name + ".sld</filename></style>"); fw.close(); RequestEntity entity = new FileRequestEntity(file, "text/xml"); post.setRequestEntity(entity); int result = client.executeMethod(post); output = result + ": " + post.getResponseBodyAsString(); } catch (Exception e) { e.printStackTrace(System.out); output = "0: " + e.getMessage(); } finally { // Release current connection to the connection pool once you are done post.releaseConnection(); } return output; }
From source file:POP3Mail.java
public static final boolean printMessageInfo(BufferedReader reader, int id, PrintWriter printWriter) throws IOException { String from = ""; String subject = ""; String received = ""; String to = ""; String replyto = ""; String date = ""; String line = null;//from w w w .j a v a2s . c om Date d = null; Date dd = null; try { dd = dateformat.parse("01 Jan 2020 00:00:00"); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } while (true) { if (line == null) { line = reader.readLine(); } if (line == null) { break; } String lower = line.toLowerCase(Locale.ENGLISH); System.out.println(line); if (lower.startsWith("from: ")) { from = line.substring(6).trim(); line = null; } else if (lower.startsWith("reply-to: ")) { replyto = line.substring("reply-to: ".length()).trim(); line = null; } else if (lower.startsWith("to: ")) { to = line.substring(3).trim(); while ((line = reader.readLine()) != null) { lower = line.toLowerCase(Locale.ENGLISH); if (line.startsWith(" ")) { to += line; } else { break; } } } else if (lower.startsWith("subject: ")) { subject = line.substring(9).trim(); line = null; } else if (lower.startsWith("date: ")) { date = line.substring("date: ".length()).trim(); try { date = date.split(",")[1].trim(); String[] tokens = date.split(" "); d = dateformat.parse(tokens[0] + " " + tokens[1] + " " + tokens[2] + " " + tokens[3]); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } line = null; } else { line = null; } } if (d != null && d.before(dd)) { JSONObject jsonObject = new JSONObject(); jsonObject.put("id", id); jsonObject.put("to", to); jsonObject.put("from", from); String[] t = parseTo(to); jsonObject.put("tolist", Arrays.asList(t)); String ttt = ""; for (String tt : t) { ttt += "," + tt; } String[] f = parseTo(from); String fff = ""; for (String ff : f) { fff += "," + ff; } jsonObject.put("fromlist", Arrays.asList(f)); jsonObject.put("subject", subject); jsonObject.put("date", date); jsonObject.put("time", d.getTime()); jsonObject.put("reply-to", replyto); FileWriter fileWriter = new FileWriter(String.format("../json/%05d.json", id)); jsonObject.write(fileWriter); fileWriter.close(); printWriter.println( String.format("%d,'%s','%s','%s','%s','%s','%s','%s',%d", id, escape(from), fff.substring(1), escape(to), ttt.substring(1), escape(replyto), escape(subject), date, d.getTime())); return true; } return false; }
From source file:com.teotigraphix.caustk.utils.RuntimeUtils.java
/** * Saves the String data to the File./*from ww w . ja va2 s .c o m*/ * * @param file The location to save the String. * @param data The String data to save. * @throws IOException */ public static final void saveFile(File file, String data) throws IOException { FileWriter writer = null; try { writer = new FileWriter(file); writer.write(data); } catch (IOException e) { throw e; } finally { writer.close(); } }
From source file:com.galenframework.ide.devices.commands.DeviceCheckLayoutCommand.java
private static File createOnePixelFakeImage() { try {//from w ww .j a v a2 s . co m File file = File.createTempFile("1-pixel-image", ".png"); FileWriter fw = new FileWriter(file); IOUtils.copy(DeviceCheckLayoutCommand.class.getResourceAsStream("/public/images/1-pixel-image.png"), fw); fw.flush(); fw.close(); return file; } catch (Exception e) { e.printStackTrace(); } return null; }