日期:2014-05-20  浏览次数:20698 次

io流总是死循环
import java.io.*;

public class FileOutputStreamTest {



public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("d:\\A.java");
fos = new FileOutputStream("d:\\s.txt");
byte [] b = new byte[32];
int len = fis.read(b);
while(len > 0){
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

}
}
}

------最佳解决方案--------------------

int len = fis.read(b);
while(len > 0){
    fos.write(b, 0, len);
    len = fis.read(b);
}

------其他解决方案--------------------
int len = fis.read(b);
while(len > 0){
    fos.write(b, 0, len);
    <span style="color: #FF0000;">len = fis.read(b);</span>
}
------其他解决方案--------------------

while((len?=?fis.read(b))!=-1)

注意资源关闭.
------其他解决方案--------------------
返回-1




-1结束