2014年5月26日星期一

About BufferedInputStream and BufferedOutputStream problems

public class Copypicture {
public static void main(String[] args) {
copy_2();//copy_1();
}
public static void copy_1(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("1.doc");
fos = new FileOutputStream("2.doc");
byte[] buff = new byte[1024];
int len = 0;
while((len=fis.read(buff))!=-1){
//System.out.println(len);
fos.write(buff,0,len);
}
System.out.println("success!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy_2(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//int i = 0;
try {
bis = new BufferedInputStream(new FileInputStream("1.doc"));
bos = new BufferedOutputStream(new FileOutputStream("2.doc"));
//byte[] buff = new byte[1024];
int len = 0;
while((len=bis.read())!=-1){//return getBufIfOpen()[pos++] & 0xff;
//System.out.println(i++);
bos.write(len);//文件有多字节就写多少次
}
System.out.println("success!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

The question is:
copy_1 () method and copy_2 () method What is the difference ? My understanding is that copy_1 () method is the size of 1024 bytes read from a text file into memory 1.doc then peremptorily write 2.doc, while copy_2 () method is to read 1024 bytes in size text into memory , then a byte by byte written to 2.doc in , is that right ? But if so, then this defines BufferedOutputStream doing, but low efficiency is not it?
------ Solution ---------------------------------------- ----
BufferedInputStream buffered input stream. It inherits FilterInputStream.
BufferedInputStream role is to add some functionality to another input stream , for example , provide a " buffering " and support for "mark () marks " and "reset () Reset method ."
BufferedInputStream essentially an internal buffer array implementation. For example, after a new input stream corresponding BufferedInputStream, when we read the data input stream by the read (), BufferedInputStream the input data stream will fill the buffer in a batch . Every time after the data buffer is read , the input stream will once again fill the data buffer ; and so forth, until we read the position of the input data stream .

copy_1 similar BufferedInputStream buff comes in buffer , copy_2 the buff that one has commented out , BufferedInputStream default buffer is 8192 bytes , regardless copy_1 in the buff, and comes with a buffer BufferedInputStream are designed to improve efficiency , as the high efficiency of the two who , know this to be tested

------ Solution ------------------------------------ --------
copy_2 you should look at is the FileInputStream BufferedInputStream wrapped up in the read (byte b [], int off, int ; len) is called many times
------ Solution ------------------------------- -------------
you such great quantities , there is no difference . But little loss .
BufferedInputStream commissioned mode.

general is read (byte b [], int off, int len), read a little , analyze , read a little .
------ Solution ---------------------------------------- ----
insight worth learning. . . mark
------ Solution --------------------------------------- -----
how many bytes to read their own control , or it may cause memory overflow

read (byte b [], int off, int len), 1024 bytes each read , write , and read again
------ For reference only ---------------------------------------
but I tested ah , copy_1 the while loop fos.write (buff, 0, len) this is OK as long as the execution of dozens , while the copy-2 of the while loop is written byte by byte , you need tens of thousands of times `` `` ` Why is this ? Since there BufferedInputStream, should not be like that in copy_1 implemented?
------ For reference only -------------------------------------- -
their top their top
------ For reference only ---------------------------- -----------

did not understand , the more points you can say /
------ For reference only ---------- -----------------------------
one is : fos.write (buff, 0, len);
Another is : bos.write (len) ;/ / file byte write how many times
method call is not the same . 1st floor
say is right
Whether copy_1 the buff, and BufferedInputStream buffers are built in order to improve efficiency
I then print the number is wrong.
specific execution order is :
BufferedInputStream the input data stream will fill in batches into the buffer . Every time after the data buffer is read , the input stream will once again fill the data buffer ; and so forth, until we read the position of the input data stream .
The FileInputStream by using copy_1 the buff effect is basically the same.
Thanks, everybody !

没有评论:

发表评论