java.io.BufferedOutputStream类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中java.io.BufferedOutputStream类的一些代码示例,展示了BufferedOutputStream类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedOutputStream类的具体详情如下:
包路径:java.io.BufferedOutputStream
类名称:BufferedOutputStream

BufferedOutputStream介绍

[英]Wraps an existing OutputStream and buffers the output. Expensive interaction with the underlying input stream is minimized, since most (smaller) requests can be satisfied by accessing the buffer alone. The drawback is that some extra space is required to hold the buffer and that copying takes place when flushing that buffer, but this is usually outweighed by the performance benefits.

A typical application pattern for the class looks like this:

BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("file.java"));

[中]

代码示例

代码示例来源:origin: stackoverflow.com

InputStream reader = new BufferedInputStream(
  object.getObjectContent());
File file = new File("localFilename");      
OutputStream writer = new BufferedOutputStream(new FileOutputStream(file));

int read = -1;

while ( ( read = reader.read() ) != -1 ) {
  writer.write(read);
}

writer.flush();
writer.close();
reader.close();

代码示例来源:origin: redisson/redisson

/**
* Write all class bytes to a file.
*
* @param fileName file where the bytes will be written
* @param bytes bytes representing the class
* @throws IOException if we fail to write the class
*/
public static void writeClass(String fileName, byte[] bytes) throws IOException {
 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
 try {
   out.write(bytes);
 }
 finally {
   out.close();
 }
}

代码示例来源:origin: stackoverflow.com

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(yourFile));
bos.write(fileBytes);
bos.flush();
bos.close();

代码示例来源:origin: pentaho/pentaho-kettle

public static String encodeBinaryData( byte[] val ) throws IOException {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 GZIPOutputStream gzos = new GZIPOutputStream( baos );
 BufferedOutputStream bos = new BufferedOutputStream( gzos );
 bos.write( val );
 bos.flush();
 bos.close();
 return new String( Base64.encodeBase64( baos.toByteArray() ) );
}

代码示例来源:origin: apache/zookeeper

public void chop() {
  File targetFile = new File(txnLogFile.getParentFile(), txnLogFile.getName() + ".chopped" + zxid);
  try (
      InputStream is = new BufferedInputStream(new FileInputStream(txnLogFile));
      OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))
  ) {
    if (!LogChopper.chop(is, os, zxid)) {
      throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Failed to chop %s", txnLogFile.getName());
    }
  } catch (Exception e) {
    System.out.println("Got exception: " + e.getMessage());
  }
}

代码示例来源:origin: redisson/redisson

final void process(Socket clnt) throws IOException {
  InputStream in = new BufferedInputStream(clnt.getInputStream());
  String cmd = readLine(in);
  logging(clnt.getInetAddress().getHostName(),
      new Date().toString(), cmd);
  while (skipLine(in) > 0){
  }
  OutputStream out = new BufferedOutputStream(clnt.getOutputStream());
  try {
    doReply(in, out, cmd);
  }
  catch (BadHttpRequest e) {
    replyError(out, e);
  }
  out.flush();
  in.close();
  out.close();
  clnt.close();
}

代码示例来源:origin: square/spoon

private static void copyFile(File source, File target) throws IOException {
 Log.d(TAG, "Will copy " + source + " to " + target);
 if (!target.createNewFile()) {
  throw new RuntimeException("Couldn't create file " + target);
 }
 chmodPlusR(target);
 BufferedInputStream is = null;
 BufferedOutputStream os = null;
 try {
  is = new BufferedInputStream(new FileInputStream(source));
  os = new BufferedOutputStream(new FileOutputStream(target));
  byte[] buffer = new byte[4096];
  while (true) {
   int read = is.read(buffer);
   if (read == -1) {
    break;
   }
   os.write(buffer, 0, read);
  }
 } finally {
  try {
   is.close();
  } catch (IOException ie) {}
  try {
   os.close();
  } catch (IOException ie) {}
 }
}

代码示例来源:origin: aws/aws-sdk-java

public void createFile(long sizeInBytes) throws IOException {
  deleteOnExit();
  FileOutputStream outputStream = new FileOutputStream(this);
  BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
  InputStream inputStream = new RandomInputStream(sizeInBytes, binaryData);
  try {
    byte[] buffer = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) > -1) {
      bufferedOutputStream.write(buffer, 0, bytesRead);
    }
  } finally {
    bufferedOutputStream.close();
    outputStream.close();
    inputStream.close();
  }
}

代码示例来源:origin: Tencent/tinker

is = new BufferedInputStream(zipFile.getInputStream(entryFile));
os = new BufferedOutputStream(new FileOutputStream(extractTo));
byte[] buffer = new byte[ShareConstants.BUFFER_SIZE];
int length = 0;
while ((length = is.read(buffer)) > 0) {
  os.write(buffer, 0, length);

代码示例来源:origin: libgdx/libgdx

private void writeFile (File outFile, byte[] bytes) {
  OutputStream out = null;
  try {
    out = new BufferedOutputStream(new FileOutputStream(outFile));
    out.write(bytes);
  } catch (IOException e) {
    throw new RuntimeException("Couldn't write file '" + outFile.getAbsolutePath() + "'", e);
  } finally {
    if (out != null) try {
      out.close();
    } catch (IOException e) {
    }
  }
}

代码示例来源:origin: loklak/loklak_server

public static void gzip(File source, File dest, boolean deleteSource) throws IOException {
  byte[] buffer = new byte[2^20];
  GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest), 65536)){{def.setLevel(Deflater.BEST_COMPRESSION);}};
  FileInputStream in = new FileInputStream(source);
  int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l);
  in.close(); out.finish(); out.close();
  if (deleteSource && dest.exists()) source.delete();
}

代码示例来源:origin: libgdx/libgdx

public void flush () {
  OutputStream out = null;
  try {
    out = new BufferedOutputStream(new FileOutputStream(file));
    properties.storeToXML(out, null);
  } catch (Exception ex) {
    throw new RuntimeException("Error writing preferences: " + file, ex);
  } finally {
    if (out != null)
      try {
        out.close();
      } catch (IOException e) {
      }
  }
}

代码示例来源:origin: k9mail/k-9

private void copyAttachmentFromFile(String resourceName, int attachmentId, int expectedFilesize) throws IOException {
  File resourceFile = new File(getClass().getResource("/attach/" + resourceName).getFile());
  File attachmentFile = new File(attachmentDir, Integer.toString(attachmentId));
  BufferedInputStream input = new BufferedInputStream(new FileInputStream(resourceFile));
  BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(attachmentFile));
  int copied = IOUtils.copy(input, output);
  input.close();
  output.close();
  Assert.assertEquals(expectedFilesize, copied);
}

代码示例来源:origin: apache/incubator-druid

/**
 * Copy from an input stream to a file (and buffer it) and close the input stream.
 * <p>
 * It is highly recommended to use FileUtils.retryCopy whenever possible, and not use a raw `InputStream`
 *
 * @param is   The input stream to copy bytes from. `is` is closed regardless of the copy result.
 * @param file The file to copy bytes to. Any parent directories are automatically created.
 *
 * @return The count of bytes written to the file
 *
 * @throws IOException
 */
public static long copyToFileAndClose(InputStream is, File file) throws IOException
{
 file.getParentFile().mkdirs();
 try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
  final long result = ByteStreams.copy(is, os);
  // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf
  os.flush();
  return result;
 }
 finally {
  is.close();
 }
}

代码示例来源:origin: neo4j/neo4j

try ( OutputStream file = new BufferedOutputStream( new FileOutputStream( new File( targetDirectory, entry.getName() ) ) ) )
        file.write( scratch, 0, read );
        toCopy -= read;
source.close();

代码示例来源:origin: Tencent/tinker

public void executeAndSaveTo(File file) throws IOException {
  OutputStream os = null;
  try {
    os = new BufferedOutputStream(new FileOutputStream(file));
    executeAndSaveTo(os);
  } finally {
    StreamUtil.closeQuietly(os);
  }
}

代码示例来源:origin: graphhopper/graphhopper

public void downloadFile(String url, String toFile) throws IOException {
  HttpURLConnection conn = createConnection(url);
  InputStream iStream = fetch(conn, false);
  int size = 8 * 1024;
  BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(toFile), size);
  InputStream in = new BufferedInputStream(iStream, size);
  try {
    byte[] buffer = new byte[size];
    int numRead;
    while ((numRead = in.read(buffer)) != -1) {
      writer.write(buffer, 0, numRead);
    }
  } finally {
    Helper.close(iStream);
    Helper.close(writer);
    Helper.close(in);
  }
}

代码示例来源:origin: Tencent/tinker

continue;
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
BufferedOutputStream bos = null;
try {
  fos = new FileOutputStream(file);
  bos = new BufferedOutputStream(fos, TypedValue.BUFFER_SIZE);
  while ((len = bis.read(buf, 0, TypedValue.BUFFER_SIZE)) != -1) {
    fos.write(buf, 0, len);
    bos.flush();
    bos.close();
    bis.close();

代码示例来源:origin: apache/ignite

/**
 * Replaces file with given body.
 *
 * @param file File to replace.
 * @param body New body for the file.
 * @throws IOException Thrown in case of any errors.
 */
private void replaceFile(String file, String body) throws IOException {
  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
    out.write(body.getBytes());
  }
}

代码示例来源:origin: stackoverflow.com

BufferedInputStream bis = new BufferedInputStream(entity.getContent());
String filePath = "sample.txt";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
int inByte;
while((inByte = bis.read()) != -1) bos.write(inByte);
bis.close();
bos.close();

相关文章