第一种方法 (不推荐) 慢
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException{
FileInputStream s=new FileInputStream("b6.png");
FileOutputStream s2=new FileOutputStream("bbb.png");
int c;
while((c =s.read()) != -1) { //不断读取每个字节
s2.write(c); //将每个字节写出
}
s.close();
s2.close();
}
}
第二种方法 (不推荐) 因为有可能导致内存溢出
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
FileInputStream F = new FileInputStream("b6.png");
FileOutputStream f2 = new FileOutputStream("lIKEEFR.png");
// int len= F.available();
// System.out.println(len);
byte[] arr = new byte[F.available()]; // 创建于文件一样大小的字节数组
F.read(arr); // 将文件上的字节读取到内存
f2.write(arr); // 将字节数组中的字节数据写到文件上
f2.close();
F.close();
}
}
1 条评论
测试测试