List of usage examples for java.io FileWriter close
public void close() throws IOException
From source file:net.itransformers.bgpPeeringMap.BgpPeeringMap.java
public static void discover() throws Exception { Map<String, String> settings = loadProperties(new File(settingsFile)); logger.info("Settings" + settings.toString()); String folderPlaceholder = settings.get("output.dir"); File outputDir = new File(projectDir + File.separator + folderPlaceholder, label); System.out.println(outputDir.getAbsolutePath()); boolean result = outputDir.mkdir(); File graphmlDir = new File(outputDir, "undirected"); result = outputDir.mkdir();/*from ww w .j a v a 2s . c o m*/ XsltTransformer transformer = new XsltTransformer(); logger.info("SNMP walk start"); byte[] rawData = snmpWalk(settings); logger.info("SNMP walk end"); File rawDataFile = new File(outputDir, "raw-data-bgpPeeringMap.xml"); FileUtils.writeStringToFile(rawDataFile, new String(rawData)); logger.info("Raw-data written to a file in folder " + outputDir); logger.info("First-transformation has started with xsltTransformator " + settings.get("xsltFileName1")); ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream(); File xsltFileName1 = new File(projectDir, settings.get("xsltFileName1")); ByteArrayInputStream inputStream1 = new ByteArrayInputStream(rawData); transformer.transformXML(inputStream1, xsltFileName1, outputStream1, settings); logger.info("First transformation finished"); File intermediateDataFile = new File(outputDir, "intermediate-bgpPeeringMap.xml"); FileUtils.writeStringToFile(intermediateDataFile, new String(outputStream1.toByteArray())); logger.trace("First transformation output"); logger.trace(outputStream1.toString()); logger.info("Second transformation started with xsltTransformator " + settings.get("xsltFileName2")); ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream(); File xsltFileName2 = new File(projectDir, settings.get("xsltFileName2")); ByteArrayInputStream inputStream2 = new ByteArrayInputStream(outputStream1.toByteArray()); transformer.transformXML(inputStream2, xsltFileName2, outputStream2, settings); logger.info("Second transformation info"); logger.trace("Second transformation Graphml output"); logger.trace(outputStream2.toString()); ByteArrayInputStream inputStream3 = new ByteArrayInputStream(outputStream2.toByteArray()); ByteArrayOutputStream outputStream3 = new ByteArrayOutputStream(); File xsltFileName3 = new File(System.getProperty("base.dir"), settings.get("xsltFileName3")); transformer.transformXML(inputStream3, xsltFileName3, outputStream3, null); File outputFile = new File(graphmlDir, "undirected-bgpPeeringMap.graphml"); FileUtils.writeStringToFile(outputFile, new String(outputStream3.toByteArray())); logger.info("Output Graphml saved in a file in" + graphmlDir); //FileUtils.writeStringToFile(nodesFileListFile, "bgpPeeringMap.graphml"); FileWriter writer = new FileWriter(new File(outputDir, "undirected" + ".graphmls"), true); writer.append("undirected-bgpPeeringMap.graphml").append("\n"); writer.close(); }
From source file:io.github.casnix.spawnerdropper.SpawnerStack.java
public static boolean TakeSpawnerOutOfService(String spawnerType) { // Read ./SpawnerDropper.SpawnerStack.json into a string // get the value of JSON->{spawnerType} and subtract 1 to it unless it is 0. // If it's zero, return false // Write file back to disk try {//from w w w. ja v a 2s .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) { return false; } else { // System.out.println("[SD DBG MSG] TSOOS 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 TakeSpawnerOutOfService(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 TakeSpawnerOutOfService(String)"); e.printStackTrace(); return false; } }
From source file:au.org.ala.spatial.util.UploadSpatialResource.java
public static String assignSld(String url, String extra, String username, String password, String data) { System.out.println("assignSld url:" + url); System.out.println("data:" + data); String output = ""; HttpClient client = new HttpClient(); client.setConnectionTimeout(10000);/* w w w. j a va 2s. c o m*/ client.setTimeout(60000); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); PutMethod put = new PutMethod(url); put.setDoAuthentication(true); // Execute the request try { // Request content will be retrieved directly // from the input stream File file = File.createTempFile("sld", "xml"); System.out.println("file:" + file.getPath()); FileWriter fw = new FileWriter(file); fw.append(data); fw.close(); RequestEntity entity = new FileRequestEntity(file, "text/xml"); put.setRequestEntity(entity); int result = client.executeMethod(put); output = result + ": " + put.getResponseBodyAsString(); } catch (Exception e) { output = "0: " + e.getMessage(); e.printStackTrace(System.out); } finally { // Release current connection to the connection pool once you are done put.releaseConnection(); } return output; }
From source file:au.org.ala.layers.util.BatchConsumer.java
private static void writeToFile(String filename, String string, boolean append) throws IOException { FileWriter fw = new FileWriter(filename, append); fw.write(string);/*ww w. j a v a 2s .c o m*/ fw.close(); }
From source file:com.bigtester.ate.tcg.controller.TrainingFileDB.java
/** * Clean test csv file.//ww w.j a v a 2 s .c om * * @throws IOException * Signals that an I/O exception has occurred. */ public static void cleanTestCsvFile() throws IOException { // initialize FileWriter object // FileSystemResource testFile = new // FileSystemResource(UserInputsTrainer.TESTFILE); FileWriter fileWriter = new FileWriter(UserInputsTrainer.TESTFILE); fileWriter.write(""); fileWriter.close(); }
From source file:edu.isistan.carcha.util.Utils.java
/** * Creates the file from string list.// ww w. j a v a 2s .co m * * @param documents the documents * @return the file * @throws IOException Signals that an I/O exception has occurred. */ public static File createFileFromStringList(List<String> documents) throws IOException { File file = null; try { file = File.createTempFile("documents_", TMP); FileWriter writer = new FileWriter(file); FileUtils.writeLines(file, documents, "\n"); writer.close(); } catch (IOException e) { logger.error("Error creating the temp file", e); throw e; } return file; }
From source file:Main.java
public static String getHardDiskSN(String drive) { String result = ""; try {//from ww w . j ava 2 s .c om File file = File.createTempFile("realhowto", ".vbs"); file.deleteOnExit(); FileWriter fw = new java.io.FileWriter(file); String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n" + "Set colDrives = objFSO.Drives\n" + "Set objDrive = colDrives.item(\"" + drive + "\")\n" + "Wscript.Echo objDrive.SerialNumber"; // see note fw.write(vbs); fw.close(); Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = input.readLine()) != null) { result += line; } input.close(); } catch (Exception e) { e.printStackTrace(); } return result.trim(); }
From source file:TestMapObjInterface.java
public static boolean runTest(String file, String fsName, String cnaddress) { try {/*from w w w. ja v a 2 s .com*/ 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:com.bluexml.side.m2.repositoryBuilder.Builder.java
private static void writeDotFile(File rootPlugins, String output) throws Exception, IOException { SideOptionsTree sot = new SideOptionsTree(); sot.extensionPointRenderer(rootPlugins); File outputFile = new File(output); FileWriter fw = new FileWriter(outputFile); DotRenderer dotRenderer = new DotRenderer(fw, sot.getParentChildren(), sot.getNode2NodeType()); dotRenderer.render();//www .j a v a2 s . c o m fw.flush(); fw.close(); }
From source file:com.dongli.model.MyJSONData.java
public static void updateObject(MyJSONObject mjo) throws MyRESTException { String uid = mjo.getUID();//from www . j a v a2 s . co m MyJSONObject myJSONObject = queryObject(uid); String path = "/tmp/" + uid; // write the JSON object to a tmp file try { FileWriter fw = new FileWriter(path, false); PrintWriter pw = new PrintWriter(fw); pw.println(mjo.toString()); pw.close(); fw.close(); } catch (IOException e) { // failed to create the new object File nf = new File(path); nf.delete(); throw new MyRESTException("Failed to update the object."); } // update the new object on Amazon AWS S3 try { File uploadFile = new File(path); MyAWSStorage.getInstance().s3client .putObject(new PutObjectRequest(MyConfiguration.getInstance().bucket, uid, uploadFile)); } catch (AmazonServiceException ase) { throw new MyRESTException("Failed to update the object " + uid + "."); } catch (AmazonClientException ace) { throw new MyRESTException("Failed to qpdate the object " + uid + "."); } }