黑马程序员技术交流社区

标题: 小分享,拷贝文件的四种方式 [打印本页]

作者: Devilbaby    时间: 2016-2-20 22:42
标题: 小分享,拷贝文件的四种方式
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, 下载次数: 26)

1.png

2.png (80.32 KB, 下载次数: 29)

2.png

3.png (75.33 KB, 下载次数: 24)

3.png

4.png (82.29 KB, 下载次数: 24)

4.png

1.png (141.89 KB, 下载次数: 22)

1.png

2.png (80.32 KB, 下载次数: 20)

2.png

3.png (75.33 KB, 下载次数: 30)

3.png

4.png (82.29 KB, 下载次数: 28)

4.png

作者: bestcaptain    时间: 2016-2-20 23:02
归纳的很好  加油
作者: Devilbaby    时间: 2016-2-24 17:39
bestcaptain 发表于 2016-2-20 23:02
归纳的很好  加油

                                       
作者: yashiro    时间: 2016-2-24 18:16
666感谢楼主分享 真的很有用!!
作者: 洋葱头头    时间: 2016-2-24 18:59
顶一个啊




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2