List of usage examples for java.io BufferedWriter BufferedWriter
public BufferedWriter(Writer out)
From source file:base.compilations.PascalCompilation.java
@Override protected void writeUserCodeInto(File file, String code) throws IOException { try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { writer.write(code);// www .j a va2 s . c o m writer.flush(); } }
From source file:com.sp.Parser.Utils.java
public static synchronized void Send_TCP(Socket s, String JsonFormattedResponse) throws IOException { BufferedWriter outToClient = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8")); outToClient.write(JsonFormattedResponse); outToClient.flush();/* www . j a v a 2 s.co m*/ }
From source file:URLEncoder.java
public static String encode(String s, String enc) throws UnsupportedEncodingException { if (!needsEncoding(s)) { return s; }// ww w . j a v a 2 s. co m int length = s.length(); StringBuffer out = new StringBuffer(length); ByteArrayOutputStream buf = new ByteArrayOutputStream(10); // why 10? // w3c says // so. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buf, enc)); for (int i = 0; i < length; i++) { int c = (int) s.charAt(i); if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == ' ') { if (c == ' ') { c = '+'; } toHex(out, buf.toByteArray()); buf.reset(); out.append((char) c); } else { try { writer.write(c); if (c >= 0xD800 && c <= 0xDBFF && i < length - 1) { int d = (int) s.charAt(i + 1); if (d >= 0xDC00 && d <= 0xDFFF) { writer.write(d); i++; } } writer.flush(); } catch (IOException ex) { throw new IllegalArgumentException(s); } } } try { writer.close(); } catch (IOException ioe) { // Ignore exceptions on close. } toHex(out, buf.toByteArray()); return out.toString(); }
From source file:com.dc.util.file.FileSupport.java
public static void writeTextFile(String folderPath, String fileName, String data, boolean isExecutable) throws IOException { File dir = new File(folderPath); if (!dir.exists()) { dir.mkdirs();/*from www . j a v a2s . com*/ } File file = new File(dir, fileName); FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { fileWriter = new FileWriter(file); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write(data); bufferedWriter.flush(); if (isExecutable) { file.setExecutable(true); } } finally { if (fileWriter != null) { fileWriter.close(); } if (bufferedWriter != null) { bufferedWriter.close(); } } }
From source file:com.testmax.util.FileUtility.java
public static void writeToFile(String path, String text) { try {/*w w w.j a v a 2 s .c o m*/ // Create file FileWriter fstream = new FileWriter(path); BufferedWriter out = new BufferedWriter(fstream); out.write(text); //Close the output stream out.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } }
From source file:com.zimbra.cs.db.MySQL.java
public static void main(String args[]) { // command line argument parsing Options options = new Options(); CommandLine cl = Versions.parseCmdlineArgs(args, options); String outputDir = cl.getOptionValue("o"); File outFile = new File(outputDir, "versions-init.sql"); outFile.delete();//from w w w . j a va 2 s. c o m try { String redoVer = com.zimbra.cs.redolog.Version.latest().toString(); String outStr = "-- AUTO-GENERATED .SQL FILE - Generated by the MySQL versions tool\n" + "USE zimbra;\n" + "INSERT INTO zimbra.config(name, value, description) VALUES\n" + "\t('db.version', '" + Versions.DB_VERSION + "', 'db schema version'),\n" + "\t('index.version', '" + Versions.INDEX_VERSION + "', 'index version'),\n" + "\t('redolog.version', '" + redoVer + "', 'redolog version')\n" + ";\nCOMMIT;\n"; Writer output = new BufferedWriter(new FileWriter(outFile)); output.write(outStr); if (output != null) output.close(); } catch (IOException e) { System.out.println("ERROR - caught exception at\n"); e.printStackTrace(); System.exit(-1); } }
From source file:com.redhat.rhn.common.util.FileUtils.java
/** * Save a String to a file on disk using specified path. * * WARNING: This deletes the original file before it writes. * * @param contents to save to file on disk * @param path to save file to.//from ww w . j a v a 2s . c om */ public static void writeStringToFile(String contents, String path) { try { File file = new File(path); if (file.exists()) { file.delete(); } file.createNewFile(); Writer output = new BufferedWriter(new FileWriter(file)); try { output.write(contents); } finally { output.close(); } } catch (Exception e) { log.error("Error trying to write file to disk: [" + path + "]", e); throw new RuntimeException(e); } }
From source file:com.gargoylesoftware.htmlunit.source.JQuery173Extractor.java
/** * Transforms the raw expectation, to the needed one by HtmlUnit. * This methods puts only the main line of the test output, without the details. * * @param input the raw file of real browser, with header and footer removed * @param output the expectation//w ww. ja v a 2 s. c o m * @throws IOException if an error occurs */ public static void extractExpectations(final File input, final File output) throws IOException { final BufferedReader reader = new BufferedReader(new FileReader(input)); final BufferedWriter writer = new BufferedWriter(new FileWriter(output)); int testNumber = 1; String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.startsWith("" + testNumber + '.') && line.endsWith("Rerun")) { line = line.substring(0, line.length() - 5); writer.write(line + "\n"); testNumber++; } } System.out.println("Last output #" + (testNumber - 1)); reader.close(); writer.close(); }
From source file:Main.java
public static void getStringFromXML(Node node, String dtdFilename, String outputFileName) throws TransformerException, IOException { File file = new File(outputFileName); if (!file.isFile()) file.createNewFile();/* ww w . java 2s. c om*/ BufferedWriter out = new BufferedWriter(new FileWriter(file)); String workingPath = System.setProperty("user.dir", file.getAbsoluteFile().getParent()); try { getStringFromXML(node, dtdFilename, out); } finally { System.setProperty("user.dir", workingPath); } out.flush(); out.close(); }
From source file:de.unisb.cs.st.javalanche.mutation.util.DBPerformanceTest.java
private static void testWrite() { StopWatch stopWatch = new StopWatch(); stopWatch.start();/*from w ww. j a v a 2 s . c om*/ try { File file = new File("test.txt"); file.deleteOnExit(); FileWriter fw = new FileWriter(file); BufferedWriter w = new BufferedWriter(fw); for (int i = 0; i < 50 * 1024 * 1024; i++) { w.append('x'); } w.close(); } catch (IOException e) { e.printStackTrace(); } stopWatch.stop(); System.out.printf("Writing file took %s\n", DurationFormatUtils.formatDurationHMS(stopWatch.getTime())); }