import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//1,拷贝文件的四种方式,推荐用哪种,不推荐用哪种,弊端是什么
public class Test01 {
public static void main(String[] args) throws IOException {
//demo1();
//demo2();
//demo3();
//demo4();
}
public static void demo4() throws FileNotFoundException, IOException {
//BufferedInputStream和BufferOutputStream拷贝 推荐
FileInputStream fis = new FileInputStream("元宵.jpg");
FileOutputStream fos = new FileOutputStream("copy2.jpg");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int b;
while((b = bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
public static void demo3() throws FileNotFoundException, IOException {
//小数组
FileInputStream fis = new FileInputStream("元宵.jpg");
FileOutputStream fos = new FileOutputStream("copy1.jpg");
byte[] arr = new byte[1024];
int len;
while((len = fis.read(arr)) != -1){
fos.write(arr, 0, len);
}
fis.close();
fos.close();
}
public static void demo2() throws FileNotFoundException, IOException {
//2.字节数组拷贝之available()方法 不推荐,会发生内存溢出
FileInputStream fis = new FileInputStream("元宵.jpg");
FileOutputStream fos = new FileOutputStream("copy.jpg");
byte[] by = new byte[fis.available()];
fis.read(by);
fos.write(by);
fis.close();
fos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
//1.一个字节
FileInputStream fis = new FileInputStream("元宵.jpg");
FileOutputStream fos = new FileOutputStream("copy4.jpg");
int d;
while((d = fis.read()) != -1){
fos.write(d);
}
fis.close();
fos.close();
}
}
|
-
1.png
(141.89 KB, 下载次数: 3)
-
2.png
(80.32 KB, 下载次数: 13)
-
3.png
(75.33 KB, 下载次数: 8)
-
4.png
(82.29 KB, 下载次数: 9)
-
1.png
(141.89 KB, 下载次数: 6)
-
2.png
(80.32 KB, 下载次数: 3)
-
3.png
(75.33 KB, 下载次数: 6)
-
4.png
(82.29 KB, 下载次数: 9)
组图打开中,请稍候......
|