List of usage examples for java.io FileWriter close
public void close() throws IOException
From source file:be.redlab.maven.yamlprops.create.YamlConvertCli.java
public static void main(String... args) throws IOException { CommandLineParser parser = new DefaultParser(); Options options = new Options(); HelpFormatter formatter = new HelpFormatter(); options.addOption(Option.builder("s").argName("source").desc("the source folder or file to convert") .longOpt("source").hasArg(true).build()); options.addOption(Option.builder("t").argName("target").desc("the target file to store in") .longOpt("target").hasArg(true).build()); options.addOption(Option.builder("h").desc("print help").build()); try {// w ww . j a va 2s .c o m CommandLine cmd = parser.parse(options, args); if (cmd.hasOption('h')) { formatter.printHelp("converter", options); } File source = new File(cmd.getOptionValue("s", System.getProperty("user.dir"))); String name = source.getName(); if (source.isDirectory()) { PropertiesToYamlConverter yamlConverter = new PropertiesToYamlConverter(); String[] ext = { "properties" }; Iterator<File> fileIterator = FileUtils.iterateFiles(source, ext, true); while (fileIterator.hasNext()) { File next = fileIterator.next(); System.out.println(next); String s = StringUtils.removeStart(next.getParentFile().getPath(), source.getPath()); System.out.println(s); String f = StringUtils.split(s, IOUtils.DIR_SEPARATOR)[0]; System.out.println("key = " + f); Properties p = new Properties(); try { p.load(new FileReader(next)); yamlConverter.addProperties(f, p); } catch (IOException e) { e.printStackTrace(); } } FileWriter fileWriter = new FileWriter( new File(source.getParentFile(), StringUtils.substringBeforeLast(name, ".") + ".yaml")); yamlConverter.writeYaml(fileWriter); fileWriter.close(); } else { Properties p = new Properties(); p.load(new FileReader(source)); FileWriter fileWriter = new FileWriter( new File(source.getParentFile(), StringUtils.substringBeforeLast(name, ".") + ".yaml")); new PropertiesToYamlConverter().addProperties(name, p).writeYaml(fileWriter); fileWriter.close(); } } catch (ParseException e) { e.printStackTrace(); formatter.printHelp("converter", options); } }
From source file:com.mysql.servlet.NewClass.java
public static void main(String[] args) { JSONObject headerFile = new JSONObject(); JSONArray listOfAttrs = new JSONArray(); listOfAttrs.add("id" + ""); listOfAttrs.add("name" + ""); listOfAttrs.add("age" + ""); listOfAttrs.add("salary" + ""); listOfAttrs.add("grade" + ""); headerFile.put("Employee", listOfAttrs); try {// www .j a v a 2 s .c o m // Writing to a file File file = new File("D:\\Employees.json"); file.createNewFile(); FileWriter fileWriter = new FileWriter(file); System.out.println("Writing JSON object to file"); System.out.println("-----------------------"); System.out.print(headerFile); fileWriter.write(headerFile.toJSONString()); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); Statement stmt = conn.createStatement(); String sqlQuery = "SELECT * FROM survey WHERE id='1'"; WebRowSet webRS = new WebRowSetImpl(); webRS.setCommand(sqlQuery);/*w w w.j av a 2 s .c o m*/ webRS.execute(conn); File file = new File("1.xml"); FileWriter fw = new FileWriter(file); System.out.println("Writing db data to file " + file.getAbsolutePath()); webRS.writeXml(fw); StringWriter sw = new StringWriter(); webRS.writeXml(sw); System.out.println(sw.toString()); fw.flush(); fw.close(); stmt.close(); conn.close(); }
From source file:LabelSampleSaveText.java
public static void main(String args[]) { JFrame frame = new JFrame("Label Focus Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); JTextField textField = new JTextField(); label.setLabelFor(textField);/*from w w w. j ava 2 s . c o m*/ panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); frame.add(panel, BorderLayout.NORTH); frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); textField.setText("your text"); String filename = "test.txt"; FileWriter writer = null; try { writer = new FileWriter(filename); textField.write(writer); } catch (IOException exception) { System.err.println("Save oops"); } finally { if (writer != null) { try { writer.close(); } catch (IOException exception) { System.err.println("Error closing writer"); exception.printStackTrace(); } } } }
From source file:contractEditor.contractHOST1.java
public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "HOST1"); obj.put("context", "VM-deployment"); //obj.put("Context", new Integer); HashMap serviceDescription = new HashMap(); serviceDescription.put("location", "France"); serviceDescription.put("certificate", "true"); serviceDescription.put("volume", "100_GB"); serviceDescription.put("price", "6_euro"); obj.put("serviceDescription", serviceDescription); HashMap gauranteeTerm = new HashMap(); gauranteeTerm.put("availability", "more_98_percentage"); obj.put("gauranteeTerm", gauranteeTerm); //Constraint1 HashMap host_rule1 = new HashMap(); HashMap VM_rule1 = new HashMap(); host_rule1.put("ID", "HOST1"); VM_rule1.put("purpose", "test"); ArrayList rule1 = new ArrayList(); rule1.add("prohibition"); rule1.add(host_rule1);//from w w w. java 2 s.com rule1.add(VM_rule1); ArrayList policyInConstraint1 = new ArrayList(); policyInConstraint1.add(rule1); ArrayList creationConstraint1 = new ArrayList(); creationConstraint1.add("other"); creationConstraint1.add("true"); creationConstraint1.add("true"); creationConstraint1.add(policyInConstraint1); ArrayList totalConstraint = new ArrayList(); totalConstraint.add(creationConstraint1); obj.put("creationConstraint", totalConstraint); try { FileWriter file = new FileWriter("confSP" + File.separator + "Host1.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print(obj); /* JSONParser parser = new JSONParser(); try { Object obj2 = parser.parse(new FileReader("confSP\\confHost1.json")); JSONObject jsonObject = (JSONObject) obj2; HashMap serviceDescription2=(HashMap) jsonObject.get("serviceDescription"); method.printHashMap(serviceDescription2); HashMap gauranteeTerm2=(HashMap) jsonObject.get("gauranteeTerm"); method.printHashMap(gauranteeTerm2); ArrayList creationConstraint=(ArrayList) jsonObject.get("creationConstraint"); method.printArrayList(creationConstraint); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } */ }
From source file:contractEditor.contractHOST2.java
public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "HOST2"); obj.put("context", "VM-deployment"); //obj.put("Context", new Integer); HashMap serviceDescription = new HashMap(); serviceDescription.put("location", "UK"); serviceDescription.put("certificate", "false"); serviceDescription.put("volume", "200_GB"); serviceDescription.put("price", "5_euro"); obj.put("serviceDescription", serviceDescription); HashMap gauranteeTerm = new HashMap(); gauranteeTerm.put("availability", "more_99_percentage"); obj.put("gauranteeTerm", gauranteeTerm); //Constraint1 HashMap host_rule1 = new HashMap(); HashMap VM_rule1 = new HashMap(); host_rule1.put("ID", "HOST2"); VM_rule1.put("data", "private"); ArrayList rule1 = new ArrayList(); rule1.add("prohibition"); rule1.add(host_rule1);//from w ww . jav a 2 s . c o m rule1.add(VM_rule1); ArrayList policyInConstraint1 = new ArrayList(); policyInConstraint1.add(rule1); ArrayList creationConstraint1 = new ArrayList(); creationConstraint1.add("other"); creationConstraint1.add("true"); creationConstraint1.add("true"); creationConstraint1.add(policyInConstraint1); ArrayList totalConstraint = new ArrayList(); totalConstraint.add(creationConstraint1); obj.put("creationConstraint", totalConstraint); try { FileWriter file = new FileWriter("confSP" + File.separator + "Host2.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print(obj); /* JSONParser parser = new JSONParser(); try { Object obj2 = parser.parse(new FileReader("confSP\\confHost1.json")); JSONObject jsonObject = (JSONObject) obj2; HashMap serviceDescription2=(HashMap) jsonObject.get("serviceDescription"); method.printHashMap(serviceDescription2); HashMap gauranteeTerm2=(HashMap) jsonObject.get("gauranteeTerm"); method.printHashMap(gauranteeTerm2); ArrayList creationConstraint=(ArrayList) jsonObject.get("creationConstraint"); method.printArrayList(creationConstraint); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } */ }
From source file:contractEditor.contractHOST3.java
public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "HOST3"); obj.put("context", "VM-deployment"); //obj.put("Context", new Integer); HashMap serviceDescription = new HashMap(); serviceDescription.put("location", "China"); serviceDescription.put("certificate", "false"); serviceDescription.put("volume", "70_GB"); serviceDescription.put("price", "4_euro"); obj.put("serviceDescription", serviceDescription); HashMap gauranteeTerm = new HashMap(); gauranteeTerm.put("availability", "more_97_percentage"); obj.put("gauranteeTerm", gauranteeTerm); //Constraint1 HashMap host_rule1 = new HashMap(); HashMap VM_rule1 = new HashMap(); host_rule1.put("ID", "HOST3"); VM_rule1.put("application", "internal"); ArrayList rule1 = new ArrayList(); rule1.add("prohibition"); rule1.add(host_rule1);// w w w. j a v a2s .c o m rule1.add(VM_rule1); ArrayList policyInConstraint1 = new ArrayList(); policyInConstraint1.add(rule1); ArrayList creationConstraint1 = new ArrayList(); creationConstraint1.add("other"); creationConstraint1.add("true"); creationConstraint1.add("true"); creationConstraint1.add(policyInConstraint1); ArrayList totalConstraint = new ArrayList(); totalConstraint.add(creationConstraint1); obj.put("creationConstraint", totalConstraint); try { FileWriter file = new FileWriter("confSP" + File.separator + "Host3.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print(obj); /* JSONParser parser = new JSONParser(); try { Object obj2 = parser.parse(new FileReader("confSP\\confHost1.json")); JSONObject jsonObject = (JSONObject) obj2; HashMap serviceDescription2=(HashMap) jsonObject.get("serviceDescription"); method.printHashMap(serviceDescription2); HashMap gauranteeTerm2=(HashMap) jsonObject.get("gauranteeTerm"); method.printHashMap(gauranteeTerm2); ArrayList creationConstraint=(ArrayList) jsonObject.get("creationConstraint"); method.printArrayList(creationConstraint); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } */ }
From source file:it.cnr.isti.smartfed.papers.qbrokage.BrokageLockin.java
public static void main(String[] args) throws IOException { String initial = "#evo_step,cost,time,used_dc \n"; String str = initial;/* w w w . jav a 2 s . c o m*/ AbstractAllocator allocator = null; int seed = 1; /* str = initial; allocator = new GreedyAllocator(); str += executeAndWrite(allocator, numDatacenters); str += "\n"; FileWriter fw1 = new FileWriter(new File("plots/lock-greedy-dc"+ dcToString() + ".dat")); fw1.write(str); fw1.flush(); fw1.close(); */ JGAPMapping.MUTATION = 10; JGAPMapping.POP_SIZE = 50; JGAPMapping.CROSSOVER = 0.35; JGAPMapping.EVOLUTION_STEP = 100; allocator = new GeneticAllocator(); str = initial; // warmup with 5 dcs allocator = new GeneticAllocator(); str += executeAndWrite(allocator, numDatacenters); str += "\n"; FileWriter fw2 = new FileWriter( new File("plots/lock-genetic-dc" + dcToString() + "cross0.35-mut10" + ".dat")); fw2.write(str); fw2.flush(); fw2.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField nameTextField = new JTextField(); frame.add(nameTextField, BorderLayout.NORTH); FileWriter writer = null; try {// w w w .j a v a 2 s . c o m writer = new FileWriter("filename.txt"); nameTextField.write(writer); } catch (IOException exception) { System.err.println("Save oops"); exception.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException exception) { System.err.println("Error closing writer"); exception.printStackTrace(); } } } frame.setSize(250, 100); frame.setVisible(true); }
From source file:com.tomgibara.cluster.CreateUniformCircles.java
public static void main(String[] args) throws IOException { UniformRandomGenerator gen = new UniformRandomGenerator(new JDKRandomGenerator()); FileWriter writer = new FileWriter("R/circles.txt"); int size = 400; try {//from w ww. j av a 2s .c om for (int y = 4; y >= -4; y--) { for (int x = -4; x <= 4; x++) { writeCluster(gen, new double[] { x, y }, new double[] { 0.3, 0.3 }, size, writer); } } } finally { writer.close(); } }