本文整理了Java中java.io.OutputStream.write()
方法的一些代码示例,展示了OutputStream.write()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OutputStream.write()
方法的具体详情如下:
包路径:java.io.OutputStream
类名称:OutputStream
方法名:write
[英]Equivalent to write(buffer, 0, buffer.length).
[中]相当于写入(buffer,0,buffer.length)。
canonical example by Tabnine
public void postRequest(String urlStr, String jsonBodyStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
try (OutputStream outputStream = httpURLConnection.getOutputStream()) {
outputStream.write(jsonBodyStr.getBytes());
outputStream.flush();
}
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
// ... do something with line
}
}
} else {
// ... do something with unsuccessful response
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public void write(OutputStream out) throws IOException {
out.write(this.bytes);
out.flush();
}
代码示例来源:origin: skylot/jadx
public static void copyStream(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[READ_BUFFER_SIZE];
while (true) {
int count = input.read(buffer);
if (count == -1) {
break;
}
output.write(buffer, 0, count);
}
}
代码示例来源:origin: stackoverflow.com
public void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
代码示例来源: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: org.testng/testng
public static void copyFile(InputStream from, File to) throws IOException {
if (! to.getParentFile().exists()) {
to.getParentFile().mkdirs();
}
try (OutputStream os = new FileOutputStream(to)) {
byte[] buffer = new byte[65536];
int count = from.read(buffer);
while (count > 0) {
os.write(buffer, 0, count);
count = from.read(buffer);
}
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Copy the contents of the given InputStream to the given OutputStream.
* Leaves both streams open when done.
* @param in the InputStream to copy from
* @param out the OutputStream to copy to
* @return the number of bytes copied
* @throws IOException in case of I/O errors
*/
public static int copy(InputStream in, OutputStream out) throws IOException {
Assert.notNull(in, "No InputStream specified");
Assert.notNull(out, "No OutputStream specified");
int byteCount = 0;
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
}
out.flush();
return byteCount;
}
代码示例来源:origin: bumptech/glide
/**
* Flushes any pending data and closes output file. If writing to an
* OutputStream, the stream is not closed.
*/
public boolean finish() {
if (!started)
return false;
boolean ok = true;
started = false;
try {
out.write(0x3b); // GIF trailer
out.flush();
if (closeStream) {
out.close();
}
} catch (IOException e) {
ok = false;
}
// reset for subsequent use
transIndex = 0;
out = null;
image = null;
pixels = null;
indexedPixels = null;
colorTab = null;
closeStream = false;
firstFrame = true;
return ok;
}
代码示例来源:origin: alibaba/nacos
public static void writeStringToFile(File file, String data, String encoding)
throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(data.getBytes(encoding));
os.flush();
} finally {
if (null != os) {
os.close();
}
}
}
代码示例来源:origin: robolectric/robolectric
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[8196];
int len;
try {
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} finally {
in.close();
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Copy the contents of the given byte array to the given OutputStream.
* Closes the stream when done.
* @param in the byte array to copy from
* @param out the OutputStream to copy to
* @throws IOException in case of I/O errors
*/
public static void copy(byte[] in, OutputStream out) throws IOException {
Assert.notNull(in, "No input byte array specified");
Assert.notNull(out, "No OutputStream specified");
try {
out.write(in);
}
finally {
try {
out.close();
}
catch (IOException ex) {
}
}
}
代码示例来源:origin: neo4j/neo4j
private File writeToFile( byte[] header, String data, Charset charset ) throws IOException
{
File file = new File( directory.directory(), "text-" + charset.name() );
try ( OutputStream out = new FileOutputStream( file );
Writer writer = new OutputStreamWriter( out, charset ) )
{
out.write( header );
writer.append( data );
}
return file;
}
代码示例来源:origin: jenkinsci/jenkins
@Override
public void run() {
try {
try {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) >= 0) {
out.write(buf, 0, len);
out.flush();
}
} finally {
in.close();
out.close();
}
} catch (IOException e) {
// TODO: what to do?
}
}
}
代码示例来源:origin: apache/rocketmq
public static void writeStringToFile(File file, String data, String encoding) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(data.getBytes(encoding));
} finally {
if (null != os) {
os.close();
}
}
}
}
代码示例来源:origin: google/data-transfer-project
void writeInputStream(String filename, InputStream inputStream) throws IOException {
File file = new File(TEMP_DIR + filename);
file.createNewFile();
try (OutputStream outputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
}
代码示例来源:origin: log4j/log4j
/**
* Copies information from the input stream to the output stream using
* the specified buffer size
* @throws java.io.IOException
*/
public static void copy(InputStream input,
OutputStream output,
int bufferSize)
throws IOException {
byte[] buf = new byte[bufferSize];
int bytesRead = input.read(buf);
while (bytesRead != -1) {
output.write(buf, 0, bytesRead);
bytesRead = input.read(buf);
}
output.flush();
}
代码示例来源:origin: alibaba/druid
public static long copy(InputStream input, OutputStream output) throws IOException {
final int EOF = -1;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
代码示例来源:origin: qiurunze123/miaosha
public static void out(HttpServletResponse res, String html){
res.setContentType("text/html");
res.setCharacterEncoding("UTF-8");
try{
OutputStream out = res.getOutputStream();
out.write(html.getBytes("UTF-8"));
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
代码示例来源:origin: prestodb/presto
@Override
public void flush()
throws IOException
{
if (_out != null) {
if (_outPtr > 0) {
_out.write(_outBuffer, 0, _outPtr);
_outPtr = 0;
}
_out.flush();
}
}
代码示例来源:origin: bumptech/glide
public static void writeFile(File file, byte[] data) throws IOException {
OutputStream out = new FileOutputStream(file);
try {
out.write(data);
out.flush();
out.close();
} finally {
try {
out.close();
} catch (IOException ex) {
// Do nothing.
}
}
}
内容来源于网络,如有侵权,请联系作者删除!