emergence of a strange question , please explain
source file text :
This is test file for copying files by java
target file text :
This is test file for copying files by javav
code below
FileInputStream input=new FileInputStream("C:\\users\\auxu\\desktop\\a.txt");
FileOutputStream output1=new FileOutputStream("C:\\users\\auxu\\desktop\\c.txt");
byte[] buffer=new byte[2];
int i=0;
while(input.available()>0){
input.read(buffer);
//System.out.println(buffer[0]+"\t"+buffer[1]);
output1.write(buffer);
System.out.println(++i);
}
Why is the text of the target file will be more of a v it ?
If the statement when the buffer length set to 64 ( or more N-th power of 2 ) , the text will be copied correctly , the length of the back NULL complement
------ Solution - ------------------------------------------
FileInputStream input=new FileInputStream("C:\\users\\auxu\\desktop\\a.txt");
FileOutputStream output1=new FileOutputStream("C:\\users\\auxu\\desktop\\c.txt");
int i=0;
while(input.available()>0){
byte[] buffer=new byte[2];
input.read(buffer);
//System.out.println(buffer[0]+"\t"+buffer[1]);
output1.write(buffer);
System.out.println(++i);
}
try
------ Solution ---------------------------------- ----------
FileInputStream input=new FileInputStream("C:\\users\\auxu\\desktop\\a.txt");
FileOutputStream output1=new FileOutputStream("C:\\users\\auxu\\desktop\\c.txt");
byte[] buffer=new byte[2];
int i=0;
int size = 0;
while(input.available()>0){
size = input.read(buffer);
//System.out.println(buffer[0]+"\t"+buffer[1]);
output1.write(buffer,0,size);
System.out.println(++i);
}
so there is not the problem ah.
------ For reference only -------------------------------------- -
There are ways you do not enter the back part
------ For reference only -------------------------- -------------
FileInputStream input=new FileInputStream("C:\\users\\auxu\\desktop\\a.txt");
FileOutputStream output1=new FileOutputStream("C:\\users\\auxu\\desktop\\c.txt");
byte[] buffer=new byte[2];
int i=0;
while(input.read(buffer)>0){
//input.read(buffer);
//System.out.println(buffer[0]+"\t"+buffer[1]);
output1.write(buffer);
System.out.println(++i);
}
------ For reference only ------------------- --------------------
buffer tune bigger ah. 1024 bar
------ For reference only ------------------------------------ ---
ah , I feel a greater buffer transfer large files will have encountered such a problem .
The key is that my target file will produce extra characters feel very strange problem
------ For reference only ------------------ ---------------------
copy out the files will still be more of a character
This a testing file for copying files by javav
------ For reference only --------------------- ------------------
This is no extra characters "v" copy came in, you can explain this problem?
------ For reference only -------------------------------------- -
you once read two bytes, the number of bytes in the original document have calculated ?
If the file length of the string is not a multiple of the number of bytes read , the compiler will still help you to read the rest of insufficient bytes !
because your original file string is singular , the last byte is not sure there is a 2 byte, it will help you to read your original file is not the character that is present on the system bar, so read more out one !
You put your original files to reduce the string last one, you see the target file has no multi- output ! ! ! Should not be , because the 2 byte just read too full , no additional read !
------ For reference only -------------------------------------- -
than purely personal speculation , if wrong , a lot of generosity !
------ For reference only -------------------------------------- -
copy out the files will still be more of a character
This a testing file for copying files by javav
Well , it seems that the time needed to read the next clean up the array
------ For reference only ------------------- --------------------
Chuan Chi podcast August 2013 javaEE + IOT cloud employment Class Share : http://pan.baidu.com/s/ 11NOyJ
------ For reference only ------------------------------------- -
This is no extra characters "v" copy came in, you can explain this problem?
Positive Solutions
------ For reference only --------------------------------- ------
I am also here to understand, but how it will read v ?
------ For reference only -------------------------------------- -
I am also here to understand, but how it will read v ? enough to add their own ! Specifically to see how it was set up !
------ For reference only ---------------------------------- -----
also help explain the great God do
------ For reference only ----------------------- ----------------
penultimate read it , buffer [0] = a, buffer [1] = v,
last read it , buffer [0] read or a, and buffer [1] did not read the content, so it is read to the previous value is : v
changed after each read when they are re- new a byte array , so there will not not read the contents of the last read the results on the use of such a situation .
------ For reference only -------------------------------------- -
program running it several times , is not likely to occur in addition to v in addition to other characters in the situation ; or you altered conditions changed (String str = input.read ()) ! = null; try
------ For reference only ------------------------------- --------
------ For reference only -------------------------------------- -
will appear only v
------ For reference only ---------------------------------- -----
completely positive solution ah. Or you put a bigger buffer threshold settings
------ For reference only ---------------------------------------
Thank you very much ! But I still redundant text buffer is copied puzzled , can explain why there is such a case ?
------ For reference only -------------------------------------- -
16 floor that is very clear. Not a copy redundant text . But in the penultimate data read and v is a two -byte array . Last read into array inside because only one a byte , and then the first one in the array read into a, the second constant , or v. Mainly because you shared outside the loop, an array . 16th floor every time a new array , the second word is 0 unchanged . With the length of the relationship, but to set a larger solve the problem.
byte[] buffer = new byte[2];
int len = 0 ;
while ((len = input.read(buffer,0,2))!=-1)
{
output1.write(buffer,0,len);
}
are generally written. 2 casual setting. Well read len is the length . That is how long can read , I write long . Do not understand why nobody wrote .
------ For reference only ---------------------------------- -----
only learned java programmers for these hard to understand , if you are a c programmers , each new initialization time in a string , not initialized every time is not likely as a random value .
没有评论:
发表评论