首页 > 代码库 > FileOutputStream flush()

FileOutputStream flush()

FileOutputStream 继承 OutputStream ,flush方法查看源码方法体为空,所以flush没起到清除缓存的作用

改用BufferedOutputStream再调用flush()能成功清除缓存

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
Date curDate = new Date(System.currentTimeMillis());
File file = new File(fileDir + sdf.format(curDate) + ".xml");
if (!file.exists()) {
    file.createNewFile();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
if (soap.length() <= 1024) {
    bos.write(soap.getBytes());
    bos.flush();
} else {
    int beginIndex = 0;
    int endIndex = 1024;
    while (beginIndex < soap.length()) {
        bos.write(soap.substring(beginIndex,
            (endIndex <= (soap.length())) ? endIndex : (soap.length())).getBytes());
        bos.flush();
        beginIndex = endIndex;
        endIndex = beginIndex + 1024;
    }
}
bos.close();

  

 

FileOutputStream flush()