Example usage for java.io BufferedWriter flush

List of usage examples for java.io BufferedWriter flush

Introduction

In this page you can find the example usage for java.io BufferedWriter flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:com.jsqix.utils.ACache.java

/**
 * ? String?  //  ww  w  . ja  v  a 2 s  .c om
 * 
 * @param key
 *            ?key
 * @param value
 *            ?String?
 */
public void put(String key, String value) {
    if (value == null)
        value = "";
    File file = mCache.newFile(key);
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter(file), 1024);
        out.write(value);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mCache.put(file);
    }
}

From source file:com.github.geequery.codegen.ast.JavaUnit.java

/**
 * ?/* w  ww.j  a  va2  s  .  co  m*/
 * 
 * @param javaFile
 * @param charset
 * @return
 * @throws IOException
 */
public File saveAs(File javaFile, Charset charset) throws IOException {
    BufferedWriter bw = IOUtils.getWriter(javaFile, charset, false);
    writeTo(bw);
    bw.flush();
    bw.close();
    return javaFile;
}

From source file:com.uber.stream.kafka.mirrormaker.controller.core.GitBackUpHandler.java

public void writeToFile(String fileName, String data) throws Exception {
    Repository backupRepo = null;//from ww w.java 2  s. c  o m
    BufferedWriter output = null;
    Git git = null;
    Git result = null;
    try {
        try {
            FileUtils.deleteDirectory(new File(localPath));
        } catch (IOException e) {
            LOGGER.error("Error deleting exisiting backup directory");
            throw e;
        }

        try {
            result = Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
        } catch (Exception e) {
            LOGGER.error("Error cloning backup git repo");
            throw e;
        }

        try {
            backupRepo = new FileRepository(localPath + "/.git");
        } catch (IOException e) {
            throw e;
        }

        git = new Git(backupRepo);
        File myfile = new File(localPath + "/" + fileName);

        try {
            output = new BufferedWriter(new FileWriter(myfile));
            output.write(data);
            output.flush();
        } catch (IOException e) {
            LOGGER.error("Error writing backup to the file with name " + fileName);
            throw e;
        }

        try {
            git.add().addFilepattern(".").call();
        } catch (GitAPIException e) {
            LOGGER.error("Error adding files to git");
            throw e;
        }

        try {
            git.commit().setMessage("Taking backup on " + new Date()).call();

        } catch (GitAPIException e) {
            LOGGER.error("Error commiting files to git");
            throw e;
        }

        try {
            git.push().call();
        } catch (GitAPIException e) {
            LOGGER.error("Error pushing files to git");
            throw e;
        }
    } catch (Exception e) {
        throw e;
    } finally {
        output.close();
        git.close();
        if (result != null)
            result.getRepository().close();
        backupRepo.close();
    }
}

From source file:org.opendatakit.utilities.ODKFileUtils.java

/**
 * TODO this is almost identical to checkOdkAppVersion
 *
 * @param appName           the app name
 * @param odkAppVersionFile the file that contains the installed version
 * @param apkVersion        the version to overwrite odkAppVerisonFile with
 *//*  w  w  w  .ja v a2 s.  c  o m*/
private static void writeConfiguredOdkAppVersion(String appName, String odkAppVersionFile, String apkVersion) {
    File versionFile = new File(getDataFolder(appName), odkAppVersionFile);

    if (!versionFile.exists()) {
        if (!versionFile.getParentFile().mkdirs()) {
            //throw new RuntimeException("Failed mkdirs on " + versionFile.getPath());
            WebLogger.getLogger(appName).e(TAG, "Failed mkdirs on " + versionFile.getParentFile().getPath());
        }
    }

    FileOutputStream fs = null;
    OutputStreamWriter w = null;
    BufferedWriter bw = null;
    try {
        fs = new FileOutputStream(versionFile, false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            w = new OutputStreamWriter(fs, StandardCharsets.UTF_8);
        } else {
            //noinspection deprecation
            w = new OutputStreamWriter(fs, Charsets.UTF_8);
        }
        bw = new BufferedWriter(w);
        bw.write(apkVersion);
        bw.write("\n");
    } catch (IOException e) {
        WebLogger.getLogger(appName).printStackTrace(e);
    } finally {
        if (bw != null) {
            try {
                bw.flush();
                bw.close();
            } catch (IOException e) {
                WebLogger.getLogger(appName).printStackTrace(e);
            }
        }
        if (w != null) {
            try {
                w.close();
            } catch (IOException e) {
                WebLogger.getLogger(appName).printStackTrace(e);
            }
        }
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                WebLogger.getLogger(appName).printStackTrace(e);
            }
        }
    }
}

From source file:io.siddhi.extension.io.file.FileSourceLineModeTestCase.java

@Test
public void siddhiIoFileTest16() throws InterruptedException, CannotRestoreSiddhiAppStateException {
    log.info("test SiddhiIoFile [mode=line] Test 16");
    String streams = "" + "@App:name('TestSiddhiApp')" + "@source(type='file', mode='line',"
            + "file.uri='file:/" + dirUri + "/line/xml/xml_line.txt', " + "tailing='true', "
            + "@map(type='xml'))" + "define stream FooStream (symbol string, price float, volume long); "
            + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    SiddhiAppRuntime siddhiAppRuntime2 = siddhiManager.createSiddhiAppRuntime(streams + query);

    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override//w w  w . j ava2 s.c  o m
        public void receive(Event[] events) {
            EventPrinter.print(events);
            int n = count.incrementAndGet();
            for (Event event : events) {
                switch (n) {
                case 1:
                    AssertJUnit.assertEquals(10000L, event.getData(2));
                    break;
                case 2:
                    AssertJUnit.assertEquals(10001L, event.getData(2));
                    break;
                case 3:
                    AssertJUnit.assertEquals(10002L, event.getData(2));
                    break;
                case 4:
                    AssertJUnit.assertEquals(10003L, event.getData(2));
                    break;
                case 5:
                    AssertJUnit.assertEquals(10004L, event.getData(2));
                    break;

                default:
                    AssertJUnit.fail();

                }
            }
        }
    });

    siddhiAppRuntime2.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            int n = count.incrementAndGet();
            for (Event event : events) {
                switch (n) {
                case 6:
                    AssertJUnit.assertEquals(1000L, event.getData(2));
                    break;
                case 7:
                    AssertJUnit.assertEquals(2000L, event.getData(2));
                    break;
                default:
                    AssertJUnit.fail();

                }
            }
        }
    });
    siddhiAppRuntime.start();
    SiddhiTestHelper.waitForEvents(waitTime, 5, count, timeout);
    byte[] snapshot = siddhiAppRuntime.snapshot();
    siddhiAppRuntime.shutdown();

    File file = new File(dirUri + "/line/xml/xml_line.txt");
    try {
        StringBuilder sb = new StringBuilder();
        sb.append("<events>").append("<event>").append("<symbol>").append("GOOGLE").append("</symbol>")
                .append("<price>").append("100").append("</price>").append("<volume>").append("1000")
                .append("</volume>").append("</event>").append("</events>\n");
        sb.append("<events>").append("<event>").append("<symbol>").append("YAHOO").append("</symbol>")
                .append("<price>").append("200").append("</price>").append("<volume>").append("2000")
                .append("</volume>").append("</event>").append("</events>");
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
        bufferedWriter.write(sb.toString());
        bufferedWriter.newLine();
        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException e) {
        log.error(e.getMessage());
    }

    siddhiAppRuntime2.restore(snapshot);
    siddhiAppRuntime2.start();
    SiddhiTestHelper.waitForEvents(waitTime, 7, count, timeout);
    //assert event count
    AssertJUnit.assertEquals("Number of events", 7, count.get());
    siddhiAppRuntime.shutdown();
}

From source file:com.neosdong.dimensionnews.Utils.ACache.java

/**
 * ? String? ??/*from  w  ww  .j  av a2s . com*/
 *
 * @param key
 *            ?key
 * @param value
 *            ?String?
 */
public void put(String key, String value) {

    if (C.APP_DEBUG) {
        Log.d(TAG, "ACache put - " + key + ":" + value);
    }

    File file = mCache.newFile(key);
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter(file), 1024);
        out.write(value);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mCache.put(file);
    }
}

From source file:io.siddhi.extension.io.file.FileSourceLineModeTestCase.java

@Test
public void siddhiIoFileTest3() throws InterruptedException {
    log.info("test SiddhiIoFile [mode=line] Test 3");
    String streams = "" + "@App:name('TestSiddhiApp')" + "@source(type='file', mode='line'," + "dir.uri='file:/"
            + dirUri + "/line/xml', " + "tailing='true', " + "@map(type='xml'))"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override/*from w ww  . java2  s. c  om*/
        public void receive(Event[] events) {
            EventPrinter.print(events);
            int n = count.incrementAndGet();
            for (Event event : events) {
                switch (n) {
                case 1:
                    AssertJUnit.assertEquals(10000L, event.getData(2));
                    break;
                case 2:
                    AssertJUnit.assertEquals(10001L, event.getData(2));
                    break;
                case 3:
                    AssertJUnit.assertEquals(10002L, event.getData(2));
                    break;
                case 4:
                    AssertJUnit.assertEquals(10003L, event.getData(2));
                    break;
                case 5:
                    AssertJUnit.assertEquals(10004L, event.getData(2));
                    break;
                case 6:
                    AssertJUnit.assertEquals(1000L, event.getData(2));
                    break;
                case 7:
                    AssertJUnit.assertEquals(2000L, event.getData(2));
                    break;
                default:
                    AssertJUnit.fail("More events received than expected.");
                }
            }
        }
    });
    Thread t1 = new Thread(new Runnable() {
        public void run() {
            siddhiAppRuntime.start();
        }
    });
    t1.start();
    SiddhiTestHelper.waitForEvents(waitTime, 5, count, timeout);
    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            File file = new File(dirUri + "/line/xml/xml_line (3rd copy).txt");
            try {
                StringBuilder sb = new StringBuilder();
                sb.append("<events>").append("<event>").append("<symbol>").append("GOOGLE").append("</symbol>")
                        .append("<price>").append("100").append("</price>").append("<volume>").append("1000")
                        .append("</volume>").append("</event>").append("</events>\n");
                sb.append("<events>").append("<event>").append("<symbol>").append("YAHOO").append("</symbol>")
                        .append("<price>").append("200").append("</price>").append("<volume>").append("2000")
                        .append("</volume>").append("</event>").append("</events>");
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                bufferedWriter.write(sb.toString());
                bufferedWriter.newLine();
                bufferedWriter.flush();
                bufferedWriter.close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
    });
    t2.start();
    SiddhiTestHelper.waitForEvents(waitTime, 7, count, timeout);
    //assert event count
    AssertJUnit.assertEquals("Number of events", 7, count.get());
    siddhiAppRuntime.shutdown();
}

From source file:io.siddhi.extension.io.file.FileSourceLineModeTestCase.java

@Test
public void siddhiIoFileTest4() throws InterruptedException {
    log.info("test SiddhiIoFile [mode=line] Test 4");
    String streams = "" + "@App:name('TestSiddhiApp')" + "@source(type='file', mode='line'," + "dir.uri='file:/"
            + dirUri + "/line/xml', " + "@map(type='xml'))"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override//  ww w. ja  va 2s  .c  o  m
        public void receive(Event[] events) {
            EventPrinter.print(events);
            int n = count.incrementAndGet();
            for (Event event : events) {
                switch (n) {
                case 1:
                    AssertJUnit.assertEquals(10000L, event.getData(2));
                    break;
                case 2:
                    AssertJUnit.assertEquals(10001L, event.getData(2));
                    break;
                case 3:
                    AssertJUnit.assertEquals(10002L, event.getData(2));
                    break;
                case 4:
                    AssertJUnit.assertEquals(10003L, event.getData(2));
                    break;
                case 5:
                    AssertJUnit.assertEquals(10004L, event.getData(2));
                    break;
                case 6:
                    AssertJUnit.assertEquals(1000L, event.getData(2));
                    break;
                case 7:
                    AssertJUnit.assertEquals(2000L, event.getData(2));
                    break;
                default:
                    AssertJUnit.fail("More events received than expected.");
                }
            }
        }
    });
    Thread t1 = new Thread(new Runnable() {
        public void run() {
            siddhiAppRuntime.start();
        }
    });
    t1.start();
    SiddhiTestHelper.waitForEvents(waitTime, 5, count, timeout);
    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            File file = new File(dirUri + "/line/xml/xml_line (3rd copy).txt");
            try {
                StringBuilder sb = new StringBuilder();
                sb.append("<events>").append("<event>").append("<symbol>").append("GOOGLE").append("</symbol>")
                        .append("<price>").append("100").append("</price>").append("<volume>").append("1000")
                        .append("</volume>").append("</event>").append("</events>\n");
                sb.append("<events>").append("<event>").append("<symbol>").append("YAHOO").append("</symbol>")
                        .append("<price>").append("200").append("</price>").append("<volume>").append("2000")
                        .append("</volume>").append("</event>").append("</events>");
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                bufferedWriter.write(sb.toString());
                bufferedWriter.newLine();
                bufferedWriter.flush();
                bufferedWriter.close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
    });
    t2.start();
    SiddhiTestHelper.waitForEvents(waitTime, 7, count, timeout);
    //assert event count
    AssertJUnit.assertEquals("Number of events", 7, count.get());
    siddhiAppRuntime.shutdown();
}

From source file:io.siddhi.extension.io.file.FileSourceLineModeTestCase.java

@Test
public void siddhiIoFileTest13() throws InterruptedException {
    log.info("test SiddhiIoFile [mode=line] Test 13");
    String streams = "" + "@App:name('TestSiddhiApp')" + "@source(type='file', mode='line',"
            + "file.uri='file:/" + dirUri + "/line/xml/xml_line.txt', " + "tailing='true', "
            + "@map(type='xml'))" + "define stream FooStream (symbol string, price float, volume long); "
            + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override/*from  w w w  .  ja  v  a2  s  . c  o  m*/
        public void receive(Event[] events) {
            EventPrinter.print(events);
            int n = count.incrementAndGet();
            for (Event event : events) {
                switch (n) {
                case 1:
                    AssertJUnit.assertEquals(10000L, event.getData(2));
                    break;
                case 2:
                    AssertJUnit.assertEquals(10001L, event.getData(2));
                    break;
                case 3:
                    AssertJUnit.assertEquals(10002L, event.getData(2));
                    break;
                case 4:
                    AssertJUnit.assertEquals(10003L, event.getData(2));
                    break;
                case 5:
                    AssertJUnit.assertEquals(10004L, event.getData(2));
                    break;
                case 6:
                    AssertJUnit.assertEquals(1000L, event.getData(2));
                    break;
                case 7:
                    AssertJUnit.assertEquals(2000L, event.getData(2));
                    break;
                default:
                    AssertJUnit.fail("More events received than expected.");
                }
            }
        }
    });
    Thread t1 = new Thread(new Runnable() {
        public void run() {
            siddhiAppRuntime.start();
        }
    });
    t1.start();
    SiddhiTestHelper.waitForEvents(waitTime, 5, count, timeout);
    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            File file = new File(dirUri + "/line/xml/xml_line.txt");
            try {
                StringBuilder sb = new StringBuilder();
                sb.append("<events>").append("<event>").append("<symbol>").append("GOOGLE").append("</symbol>")
                        .append("<price>").append("100").append("</price>").append("<volume>").append("1000")
                        .append("</volume>").append("</event>").append("</events>\n");
                sb.append("<events>").append("<event>").append("<symbol>").append("YAHOO").append("</symbol>")
                        .append("<price>").append("200").append("</price>").append("<volume>").append("2000")
                        .append("</volume>").append("</event>").append("</events>");
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                bufferedWriter.write(sb.toString());
                bufferedWriter.newLine();
                bufferedWriter.flush();
                bufferedWriter.close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
    });
    t2.start();
    SiddhiTestHelper.waitForEvents(waitTime, 7, count, timeout);
    //assert event count
    AssertJUnit.assertEquals("Number of events", 7, count.get());
    siddhiAppRuntime.shutdown();
}

From source file:com.emin.digit.mobile.android.storage.cache.FileCache.java

/**
 * ? String?  /* w  w w  .j  a  v  a2 s .  co m*/
 * 
 * @param key
 *            ?key
 * @param value
 *            ?String?
 */
public void put(String key, String value) {
    debug("11 --- key:" + key + " value:" + value);
    File file = mCacheManager.newFile(key);

    debug("13 --- file.getAbsolutePath()" + file.getAbsolutePath());

    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter(file), 1024);
        out.write(value);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mCacheManager.put(file);
    }
}