List of usage examples for java.io FileWriter close
public void close() throws IOException
From source file:com.serotonin.m2m2.web.dwr.ModulesDwr.java
@DwrPermission(admin = true) synchronized public static void scheduleRestart() { if (RESTART_TASK == null) { SystemEventType.raiseEvent(new SystemEventType(SystemEventType.TYPE_SYSTEM_SHUTDOWN), System.currentTimeMillis(), false, new TranslatableMessage("modules.restartScheduledBy", Common.getUser().getUsername())); long timeout = Common.getMillis(Common.TimePeriods.SECONDS, 10); RESTART_TASK = new TimeoutTask(timeout, new TimeoutClient() { @Override//from w ww. ja va2 s . c o m public void scheduleTimeout(long fireTime) { File restartFlag = new File(Common.MA_HOME, "RESTART"); if (!restartFlag.exists()) { try { FileWriter fw = new FileWriter(restartFlag); fw.write("restart"); fw.close(); } catch (IOException e) { LOG.error("Unabled to create restart flag file", e); } } Providers.get(ILifecycle.class).terminate(); } }); } }
From source file:Main.java
public static void createTestDataFile(String filename, String[] lines) throws IOException { FileWriter writer = null; try {/* w w w . ja v a 2 s.co m*/ File file = new File(filename); file.deleteOnExit(); writer = new FileWriter(file); for (String line : lines) { writer.write(line + "\n"); } } finally { if (writer != null) { writer.close(); } } }
From source file:com.aistor.generate.Generate.java
/** * /*from w w w .jav a 2 s .c om*/ * @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("??"); } } catch (Exception e) { e.printStackTrace(); } }
From source file:io.github.casnix.spawnerdropper.SpawnerStack.java
public static boolean PutSpawnerIntoService(String spawnerType) { // Read ./SpawnerDropper.SpawnerStack.json into a string // get the value of JSON->{spawnerType} and add 1 to it. // Write file back to disk try {/* w ww . ja va2 s . c o m*/ // Read entire ./SpawnerDropper.SpawnerStack.json into a string String spawnerStack = new String( Files.readAllBytes(Paths.get("./plugins/SpawnerDropper/SpawnerStack.json"))); // get the value of JSON->{spawnerType} JSONParser parser = new JSONParser(); Object obj = parser.parse(spawnerStack); JSONObject jsonObj = (JSONObject) obj; long numberInService = (Long) jsonObj.get(spawnerType); if (numberInService < 0) { numberInService = 0; } // System.out.println("[SD DBG MSG] PSIS numberInServer("+numberInService+")"); numberInService += 1; jsonObj.put(spawnerType, new Long(numberInService)); FileWriter file = new FileWriter("./plugins/SpawnerDropper/SpawnerStack.json"); file.write(jsonObj.toJSONString()); file.flush(); file.close(); return true; } catch (ParseException e) { Bukkit.getLogger().warning("[SpawnerDropper] Caught ParseException in PutSpawnerIntoService(String)"); e.printStackTrace(); return false; } catch (FileNotFoundException e) { Bukkit.getLogger() .warning("[SpawnerDropper] Could not find ./plugins/SpawnerDropper/SpawnerStack.json"); e.printStackTrace(); return false; } catch (IOException e) { Bukkit.getLogger().warning("[SpawnerDropper] Caught IOException in PutSpawnerIntoService(String)"); e.printStackTrace(); return false; } }
From source file:io.fabric8.tooling.archetype.generator.ArchetypeTest.java
@AfterClass public static void afterAll() throws Exception { // now let invoke the projects final int[] resultPointer = new int[1]; StringWriter sw = new StringWriter(); Set<String> modules = new HashSet<String>(); for (final String outDir : outDirs) { String module = new File(outDir).getName(); if (modules.add(module)) { sw.append(String.format(" <module>%s</module>\n", module)); }/*from ww w. j a v a2 s .co m*/ } sw.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(ArchetypeTest.class.getResourceAsStream("/archetypes-test-pom.xml"), baos); String pom = new String(baos.toByteArray()).replace(" <!-- to be replaced -->", sw.toString()); FileWriter modulePom = new FileWriter("target/archetypes-test-pom.xml"); IOUtils.copy(new StringReader(pom), modulePom); modulePom.close(); final String outDir = new File("target").getCanonicalPath(); // thread locals are evil (I'm talking to you - org.codehaus.plexus.DefaultPlexusContainer#lookupRealm!) Thread t = new Thread(new Runnable() { @Override public void run() { System.out.println("Invoking projects in " + outDir); MavenCli maven = new MavenCli(); resultPointer[0] = maven.doMain( new String[] { "clean", "package", "-f", "archetypes-test-pom.xml" }, outDir, System.out, System.out); System.out.println("result: " + resultPointer[0]); } }); t.start(); t.join(); assertEquals("Build of project " + outDir + " failed. Result = " + resultPointer[0], 0, resultPointer[0]); }
From source file:de.tudarmstadt.ukp.lmf.transform.sensealignments.FrameNetWiktionaryAlignment.java
/** * /* w ww . j a v a2s .co m*/ * @param classifierOut - output tsv of weka classification * @param classifierIn - data section of arff input file for classification * (contains ids in same order as classifications in classifierOut) * @param tsvFile * @throws IOException */ public static void classifierOutputToTsv(String classifierOut, String classifierIn, String tsvFile) throws IOException { List<String> res = new ArrayList<String>(); // read classification InputStream is = new BufferedInputStream(new FileInputStream(new File(classifierOut))); Reader reader = new InputStreamReader(is); BufferedReader br = new BufferedReader(reader); String line = br.readLine(); line = br.readLine(); ArrayList<String> scoreLines = new ArrayList<String>(); while (line != null) { scoreLines.add(line); line = br.readLine(); System.out.println(scoreLines.size()); } // read ids InputStream is2 = new BufferedInputStream(new FileInputStream(new File(classifierIn))); Reader reader2 = new InputStreamReader(is2); BufferedReader br2 = new BufferedReader(reader2); String line2 = br2.readLine(); line2 = br2.readLine(); ArrayList<String> idLines = new ArrayList<String>(); while (line2 != null) { idLines.add(line2); line2 = br2.readLine(); } System.out.println(scoreLines.size()); System.out.println(idLines.size()); if (scoreLines.size() != idLines.size()) {// logger.warn("files do not agree"); } int positive = 0; int negative = 0; for (int i = 0; i < scoreLines.size(); i++) { String[] scoreitems = scoreLines.get(i).split(":"); String[] iditems = idLines.get(i).split(","); String first = iditems[0]; String second = iditems[1]; String sysScore = scoreitems[2].split(",")[0]; if (sysScore.equals("1")) {// pair classified as alignment res.add(first + "\t" + second); positive++; } else { negative++; } } logger.info("positive class-->added as alignment: " + positive); logger.info("negative class-->no alignment: " + negative); System.out.println("write positive class to file"); FileWriter fw = new FileWriter(new File(tsvFile)); for (String r : res) { fw.write(r + "\n"); } fw.close(); br2.close(); br.close(); }
From source file:com.testmax.util.FileUtility.java
public static void createTextFile(String absolute_path, String text) { System.out.println("Creating text file at: " + absolute_path); WmLog.getCoreLogger().info("Creating text file at: " + absolute_path); try {/*w w w . j a va 2s .co m*/ FileWriter file = new FileWriter(new File(absolute_path)); file.write(text); file.close(); WmLog.getCoreLogger().info("Saved text file at: " + absolute_path); } catch (IOException e) { // TODO Auto-generated catch block WmLog.getCoreLogger().info("Problem occured while saving the HTML Report file html_report.html for " + absolute_path + " " + e.getMessage()); } }
From source file:com.dragovorn.dragonbot.api.bot.file.FileManager.java
public static void setDirectory(String directory) { File file = new File(directory, "Dragon Bot"); if (FileManager.directory.exists() && !file.exists()) { if (file.mkdirs()) { try { FileUtils.copyDirectory(FileManager.directory, file); } catch (IOException exception) { exception.printStackTrace(); }/*www .j a v a2 s . c o m*/ } else { throw new RuntimeException("Unable to create file: " + file.getName()); // FIXME: 11/23/16 make custom exception } try { FileUtils.deleteDirectory(FileManager.directory); } catch (IOException exception) { exception.printStackTrace(); } } FileManager.directory = file; File path = new File(FileManager.dir, "path"); try { FileWriter fileWriter = new FileWriter(path); BufferedWriter writer = new BufferedWriter(fileWriter); writer.write(directory); writer.close(); fileWriter.close(); } catch (IOException exception) { exception.printStackTrace(); } reloadFiles(); }
From source file:Main.java
public static boolean writeFile(String filePath, String content, boolean append) { if (TextUtils.isEmpty(content)) { return false; }/* ww w .j a v a2 s . c o m*/ FileWriter fileWriter = null; try { makeDirs(filePath); fileWriter = new FileWriter(filePath, append); fileWriter.write(content); fileWriter.close(); return true; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } }
From source file:fridgegameinstaller.MCJsonConf.java
public static void getJson(String path, int mb) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://api.fridgegame.com/mcconf/Fridgegame.json"); CloseableHttpResponse response1 = httpclient.execute(httpGet); try {/*from ww w. j a v a2 s .c o m*/ System.out.println(httpGet.toString()); System.out.println(response1.getStatusLine()); BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent())); String a; String output = ""; while ((a = br.readLine()) != null) { output += a + "\n"; } System.out.println(output); try { JSONObject json = new JSONObject(output); String mcArgs = json.getString("minecraftArguments"); String regex = "(-Xmx[^ ]*)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(mcArgs); String newArgs = m.replaceAll("-Xmx" + mb + "M"); json.put("minecraftArguments", newArgs); FileWriter file = new FileWriter(path); try { file.write(json.toString()); } catch (IOException e) { e.printStackTrace(); } finally { file.flush(); file.close(); } } catch (JSONException e) { } } finally { response1.close(); } }