- Java code
public class Stream { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream(new File("F:\\a.exe")); FileOutputStream out = new FileOutputStream(new File("C:\\b.exe")); System.out.println(in.available()); byte buf[] = new byte[1024]; int size = -1; int sum = in.available(); while ((size=in.read(buf,0,buf.length)) != -1) { if(size < 1024){// System.out.println("再写入-->"+size); out.write(size); }else{// System.out.println("写入-->" + size); out.write(buf); } } out.flush(); out.close(); in.close(); }}
b.exe 总是会比 a.exe 小那么几百个字节,不知道这样写是否正确???请教高手。。。
------解决方案--------------------
不知道你想干什么,如果只是复制文件,保持文件大小一致的话:
- Java code
public class Stream { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream(new File("F:\\a.exe")); FileOutputStream out = new FileOutputStream(new File("C:\\b.exe")); System.out.println(in.available()); byte buf[] = new byte[1024]; int size = -1; int sum = in.available(); while ((size=in.read(buf,0,buf.length)) != -1) { out.write(buf,0,size); } out.flush(); out.close(); in.close(); }}