List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
public static void main(String[] args) throws IOException { String letters = "from java2s.com"; StringWriter sw = new StringWriter(); BufferedWriter bw = new BufferedWriter(sw); for (char c : letters.toCharArray()) { bw.append(c);/*from ww w .j a v a 2s .c o m*/ bw.flush(); System.out.println(sw.getBuffer()); } }
From source file:Main.java
public static void main(String[] args) throws IOException { char[] cbuf = "from java2s.com".toCharArray(); StringWriter sw = new StringWriter(); BufferedWriter bw = new BufferedWriter(sw); bw.write(cbuf, 2, 5);//from w ww . ja v a2 s . c o m bw.flush(); StringBuffer sb = sw.getBuffer(); System.out.println(sb); }
From source file:Main.java
public static void main(String[] args) throws IOException { String str = "from java2s.com!"; // create string writer StringWriter sw = new StringWriter(); // create buffered writer BufferedWriter bw = new BufferedWriter(sw); // writing string to writer bw.write(str, 1, 4);/*from w w w . j a v a2 s .co m*/ // forces out the characters to string writer bw.flush(); // string buffer is created StringBuffer sb = sw.getBuffer(); System.out.println(sb); }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLOutputFactory xof = XMLOutputFactory.newFactory(); StringWriter sw = new StringWriter(); XMLStreamWriter xsw = xof.createXMLStreamWriter(sw); xsw.writeStartDocument();//w w w . j a v a 2s . c o m xsw.writeStartElement("foo"); xsw.writeCharacters("<>\"&'"); xsw.writeEndDocument(); String xml = sw.toString(); System.out.println(xml); // READ THE XML XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); xsr.nextTag(); // Advance to "foo" element System.out.println(xsr.getElementText()); }
From source file:Main.java
public static void main(String[] args) { JsonObject personObject = Json.createObjectBuilder().add("name", "Jack").add("age", 13) .add("isMarried", false) .add("address", Json.createObjectBuilder().add("street", "Main Street").add("city", "New York") .add("zipCode", "11111").build()) .add("phoneNumber", Json.createArrayBuilder().add("00-000-0000").add("11-111-1111").add("11-111-1112").build()) .build();// w w w . j a v a 2s . c om StringWriter stringWriter = new StringWriter(); JsonWriter writer = Json.createWriter(stringWriter); writer.writeObject(personObject); writer.close(); System.out.println(stringWriter.getBuffer().toString()); }
From source file:CompileSourceInMemory.java
public static void main(String args[]) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); StringWriter writer = new StringWriter(); PrintWriter out = new PrintWriter(writer); out.println("public class HelloWorld {"); out.println(" public static void main(String args[]) {"); out.println(" System.out.println(\"This is in another java file\");"); out.println(" }"); out.println("}"); out.close();/*from ww w. ja va2 s . com*/ JavaFileObject file = new JavaSourceFromString("HelloWorld", writer.toString()); Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file); CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits); boolean success = task.call(); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { System.out.println(diagnostic.getCode()); System.out.println(diagnostic.getKind()); System.out.println(diagnostic.getPosition()); System.out.println(diagnostic.getStartPosition()); System.out.println(diagnostic.getEndPosition()); System.out.println(diagnostic.getSource()); System.out.println(diagnostic.getMessage(null)); } System.out.println("Success: " + success); if (success) { try { Class.forName("HelloWorld").getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null }); } catch (ClassNotFoundException e) { System.err.println("Class not found: " + e); } catch (NoSuchMethodException e) { System.err.println("No such method: " + e); } catch (IllegalAccessException e) { System.err.println("Illegal access: " + e); } catch (InvocationTargetException e) { System.err.println("Invocation target: " + e); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { MessageRS message = new MessageRS(); message.setUserId(10);/*from w w w .ja v a2 s. c o m*/ MessageRS.Bucket bucket1 = new MessageRS.Bucket(); bucket1.setText("This is sample text"); bucket1.setChannels(Arrays.asList("Test A", "Test B")); MessageRS.Bucket bucket2 = new MessageRS.Bucket(); bucket2.setText("Some more text"); bucket2.setChannels(Arrays.asList("1", "2")); message.setBuckets(Arrays.asList(bucket1, bucket2)); StringWriter writer = new StringWriter(); JAXBContext.newInstance(MessageRS.class).createMarshaller().marshal(message, writer); System.out.println(writer); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder(); Document node = documentBuilder.parse(new FileInputStream("data.xml")); cleanEmptyTextNodes(node);//from w ww. j ava2s . c om StreamResult result = new StreamResult(new StringWriter()); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(4)); transformer.transform(new DOMSource(node), result); System.out.println(result.getWriter().toString()); }
From source file:InputOutputDemoString.java
public static void main(String[] a) throws Exception { //Read from a String s as if it were a text file: Reader r = new StringReader("abc"); System.out.println("abc: " + (char) r.read() + (char) r.read() + (char) r.read()); //Write to a StringBuffer as if it were a text file: Writer sw = new StringWriter(); sw.write('d'); sw.write('e'); sw.write('f'); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st = conn.createStatement();/*from w w w.j a v a2 s . c o m*/ String query = "select * from survey where 1 = 0"; WebRowSet webRS = new WebRowSetImpl(); webRS.setCommand(query); webRS.execute(conn); // convert xml to a String object StringWriter sw = new StringWriter(); webRS.writeXml(sw); System.out.println(sw.toString()); st.close(); conn.close(); }