package test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.LineNumberReader; import java.math.BigDecimal; import java.nio.channels.NonReadableChannelException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import privateclass.MyBufferedReader; public class Main { private static final String space_operator = " "; private static final double pi = Math.PI; private static final String LINE_SEPARATOR = System.getProperty("line.separator"); public static void main(String[] args) throws Exception { demo_1(); demo_2(); demo_3(); demo_4(); } private static void demo_1() throws Exception { FileInputStream fis = new FileInputStream("d:\\daxiao.mp4"); FileOutputStream fos = new FileOutputStream("d:\\Copy_daxiao1.mp4"); byte [] buf = new byte[1024]; int len = 0; while((len = fis.read(buf)) != -1) { fos.write(buf,0,len); } fis.close(); fos.close(); } /* * 这个方法是一个字节一个字节的读取 * 很慢不建议使用 */ private static void demo_2() throws Exception { FileInputStream fis = new FileInputStream("d:\\daxiao.mp4"); FileOutputStream fos = new FileOutputStream("d:\\Copy_daxiao2.mp4"); int len = 0; while((len = fis.read()) != -1) { fos.write(len); } fis.close(); fos.close(); } private static void demo_3() throws Exception { FileInputStream fis = new FileInputStream("d:\\daxiao.mp4"); FileOutputStream fos = new FileOutputStream("d:\\Copy_daxiao3.mp4"); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); int len = 0; while((len = bis.read()) != -1) { bos.write(len); } bis.close(); bos.close(); } /* * 缓冲区的大小就是我们所读文件的大小 * 但是当文件太大的时候我们就要考虑爆数组的问题了 */ private static void demo_4() throws Exception { FileInputStream fis = new FileInputStream("d:\\daxiao.mp4"); FileOutputStream fos = new FileOutputStream("d:\\Copy_daxiao4.mp4"); byte [] buf = new byte[fis.available()]; fis.read(buf); fos.write(buf); fis.close(); fos.close(); } }
原文:https://www.cnblogs.com/WINDZLY/p/11788803.html