java 中的字节写文件是什么概念?
What is concept of write files with bytes in java?
我知道当我想写一个文件时,我应该使用这样的代码:
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream("/sdcard/myfile.jpg");
byte Data[] = new byte[1024];
long total = 0;
while ((count=inputStream.read(Data)) != -1) {
total = total+count;
outputStream.write(Data,0,count);
}
但是我不明白这个结果是怎么回事,它是在写入一个从 0 到 100 的图像文件吗?
任何人都可以为我描述这是怎么发生的吗?谢谢
该代码将从您的 URL 下载一个文件并将其保存为 /sdcard/myfile.jpg,如果这正是您所要求的。
plz take care i just want concept of write files. this code is sample and not really full code
让我们从检查非缓冲 IO 开始。
try (InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream("/sdcard/myfile.jpg")) {
int b;
while ((b = inputStream.read()) != -1) {
outputStream.write(b);
}
}
这将一次读取和写入一个字节。大多数物理设备都支持缓冲(一种或另一种形式)。他们实际上可以一次读取多个字节(或传输一个包含多个字节的数据包)。在这种情况下,使用缓冲区读取更有效(记住 close
在 finally
块中,或使用 try-with-resources
Statement.
tl;dr
非缓冲 IO 一次读取一个字节(对于一兆字节,将调用一百万次读取)。缓冲 IO 读取多个字节作为 块 以优化或减少 read
(s)。
此代码所做的是将 1024 个字节读入临时缓冲区,将它们写入输出,然后重复,用输入的下一个数据覆盖缓冲区的内容,直到没有更多数据。
代码功能的一些细分:
//get an InputStream from a URL, so we can download the data from it.
InputStream inputStream = new BufferedInputStream(url.openStream());
//Create an OutputStream so we can write the data we get from URL to the file
OutputStream outputStream = new FileOutputStream("/sdcard/myfile.jpg");
//create a temporary buffer to store the bytes we get from the URL
byte Data[] = new byte[1024];
long total = 0; //-< you don't actually use this anywhere..
//here, you read your InputStream (URL), into your temporary buffer, and you
//also increment the count variable based on the number of bytes read.
//If there is no more data (read(...) returns -1) exit the loop, as we are finished.
while ((count=inputStream.read(Data)) != -1) {
total = total+count; //this is not actually used.. Also, where did you define "count" ?
//write the content of the byte array "buffer" to the outputStream.
//the "count" variable tells the outputStream how much you want to write.
outputStream.write(Data,0,count);
//After, do the whole thing again.
}
有趣的是循环控制语句:
while ((count=inputStream.read(Data)) != -1)
这里发生的是在循环控制语句内部赋值。这相当于这个,只是更短:
count = inputStream.read(Data);
while(count != -1) {
...do something
count = inputStream.read(Data);
}
这里的另一件事是 inputStream.read(Data)
。这会将字节读入临时缓冲区 (Data
),并且它 也 returns 读取的字节数,或者 -1
如果有none 离开了。这使我们能够以一种简单的方式传输数据,就像这里一样,通过检查 read(...)
的 return 值来控制数据的写入方式并在剩余 none 时停止。
请注意,您应该始终 关闭 您的 InputStream
和 OutputStream
不再需要时,因为您最终可能会损坏或不完整否则归档。您可以使用 .close()
方法。
我知道当我想写一个文件时,我应该使用这样的代码:
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream("/sdcard/myfile.jpg");
byte Data[] = new byte[1024];
long total = 0;
while ((count=inputStream.read(Data)) != -1) {
total = total+count;
outputStream.write(Data,0,count);
}
但是我不明白这个结果是怎么回事,它是在写入一个从 0 到 100 的图像文件吗?
任何人都可以为我描述这是怎么发生的吗?谢谢
该代码将从您的 URL 下载一个文件并将其保存为 /sdcard/myfile.jpg,如果这正是您所要求的。
plz take care i just want concept of write files. this code is sample and not really full code
让我们从检查非缓冲 IO 开始。
try (InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream("/sdcard/myfile.jpg")) {
int b;
while ((b = inputStream.read()) != -1) {
outputStream.write(b);
}
}
这将一次读取和写入一个字节。大多数物理设备都支持缓冲(一种或另一种形式)。他们实际上可以一次读取多个字节(或传输一个包含多个字节的数据包)。在这种情况下,使用缓冲区读取更有效(记住 close
在 finally
块中,或使用 try-with-resources
Statement.
tl;dr
非缓冲 IO 一次读取一个字节(对于一兆字节,将调用一百万次读取)。缓冲 IO 读取多个字节作为 块 以优化或减少 read
(s)。
此代码所做的是将 1024 个字节读入临时缓冲区,将它们写入输出,然后重复,用输入的下一个数据覆盖缓冲区的内容,直到没有更多数据。
代码功能的一些细分:
//get an InputStream from a URL, so we can download the data from it.
InputStream inputStream = new BufferedInputStream(url.openStream());
//Create an OutputStream so we can write the data we get from URL to the file
OutputStream outputStream = new FileOutputStream("/sdcard/myfile.jpg");
//create a temporary buffer to store the bytes we get from the URL
byte Data[] = new byte[1024];
long total = 0; //-< you don't actually use this anywhere..
//here, you read your InputStream (URL), into your temporary buffer, and you
//also increment the count variable based on the number of bytes read.
//If there is no more data (read(...) returns -1) exit the loop, as we are finished.
while ((count=inputStream.read(Data)) != -1) {
total = total+count; //this is not actually used.. Also, where did you define "count" ?
//write the content of the byte array "buffer" to the outputStream.
//the "count" variable tells the outputStream how much you want to write.
outputStream.write(Data,0,count);
//After, do the whole thing again.
}
有趣的是循环控制语句:
while ((count=inputStream.read(Data)) != -1)
这里发生的是在循环控制语句内部赋值。这相当于这个,只是更短:
count = inputStream.read(Data);
while(count != -1) {
...do something
count = inputStream.read(Data);
}
这里的另一件事是 inputStream.read(Data)
。这会将字节读入临时缓冲区 (Data
),并且它 也 returns 读取的字节数,或者 -1
如果有none 离开了。这使我们能够以一种简单的方式传输数据,就像这里一样,通过检查 read(...)
的 return 值来控制数据的写入方式并在剩余 none 时停止。
请注意,您应该始终 关闭 您的 InputStream
和 OutputStream
不再需要时,因为您最终可能会损坏或不完整否则归档。您可以使用 .close()
方法。