天天看点

java 文件输入输出流 文件的复制

package com.enation.test.shop.cart;

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream;

public class Test {

public static void main(String[] args) { // TODO Auto-generated method stub copyFile("D:/o2o/linux服务器数据实时同步技术.docx","D:/spring/linux服务器数据实时同步技术.docx");

}

public static void copyFile(String oldPath, String newPath) {        try {            int bytesum = 0;            int byteread = 0;            File oldfile = new File(oldPath);            if (oldfile.exists()) {                  //文件存在时                InputStream inStream = new FileInputStream(oldPath);      //读入原文件                FileOutputStream fs = new FileOutputStream(newPath);                byte[] buffer = new byte[1444];                int length;                while ( (byteread = inStream.read(buffer)) != -1) {                    bytesum += byteread;            //字节数 文件大小                    System.out.println(bytesum);                    fs.write(buffer, 0, byteread);                }                inStream.close();            }        }  catch (Exception e) {            System.out.println("复制单个文件操作出错");            e.printStackTrace();        }    }  }