java 文件输入输出流 文件的复制
2016-02-24 20:44
761 查看
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 {
/**
* @param args
*/
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();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class Test {
/**
* @param args
*/
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();
}
}
}
相关文章推荐
- 13. Roman to Integer
- SSM——Spring
- Struts2之文件下载
- java 文件输入输出流 文件夹的复制
- java中抽象的(abstract)方法是否可同时是静态的(static),是否可同时是本地方法(native),是否可同时被synchronized修饰
- Struts2之文件上传
- Java-反射机制学习
- Spring中自己主动装配
- Java中Comparable和Comparator区别小结
- 206. Reverse Linked List
- Java类名与文件名关系
- Java NIO:浅析I/O模型
- java调用cmd命令 exp导出数据库表格数据
- java调用cmd命令 exp导出数据库表格数据
- spring MVC访问静态文件如 .css .js
- java生产者消费者模式练习
- java反射机制详解
- eclipse创建avd
- 4 java语句 4.2条件语句
- 上机题目(初级)- 数据库事务(Java)