文件的拷贝
2012-07-18 14:25
78 查看
import java.io.* ;
public class Copy{
public static void main(String args[]){
if(args.length!=2){ // 判断是否是两个参数
System.out.println("输入的参数不正确。") ;
System.out.println("例:java Copy 源文件路径 目标文件路径") ;
System.exit(1) ; // 系统退出
}
File f1 = new File(args[0]) ; // 源文件的File对象
File f2 = new File(args[1]) ; // 目标文件的File对象
if(!f1.exists()){
System.out.println("源文件不存在!") ;
System.exit(1) ;
}
InputStream input = null ; // 准备好输入流对象,读取源文件
OutputStream out = null ; // 准备好输出流对象,写入目标文件
try{
input = new FileInputStream(f1) ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
try{
out = new FileOutputStream(f2) ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
if(input!=null && out!=null){ // 判断输入或输出是否准备好
int temp = 0 ;
try{
while((temp=input.read())!=-1){ // 开始拷贝
out.write(temp) ; // 边读边写
}
System.out.println("拷贝完成!") ;
}catch(IOException e){
e.printStackTrace() ;
System.out.println("拷贝失败!") ;
}
try{
input.close() ; // 关闭
out.close() ; // 关闭
}catch(IOException e){
e.printStackTrace() ;
}
}
}
}
public class Copy{
public static void main(String args[]){
if(args.length!=2){ // 判断是否是两个参数
System.out.println("输入的参数不正确。") ;
System.out.println("例:java Copy 源文件路径 目标文件路径") ;
System.exit(1) ; // 系统退出
}
File f1 = new File(args[0]) ; // 源文件的File对象
File f2 = new File(args[1]) ; // 目标文件的File对象
if(!f1.exists()){
System.out.println("源文件不存在!") ;
System.exit(1) ;
}
InputStream input = null ; // 准备好输入流对象,读取源文件
OutputStream out = null ; // 准备好输出流对象,写入目标文件
try{
input = new FileInputStream(f1) ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
try{
out = new FileOutputStream(f2) ;
}catch(FileNotFoundException e){
e.printStackTrace() ;
}
if(input!=null && out!=null){ // 判断输入或输出是否准备好
int temp = 0 ;
try{
while((temp=input.read())!=-1){ // 开始拷贝
out.write(temp) ; // 边读边写
}
System.out.println("拷贝完成!") ;
}catch(IOException e){
e.printStackTrace() ;
System.out.println("拷贝失败!") ;
}
try{
input.close() ; // 关闭
out.close() ; // 关闭
}catch(IOException e){
e.printStackTrace() ;
}
}
}
}
相关文章推荐
- mysql 备份(直接拷贝数据文件)仅限myisam数据库可以
- android中拷贝assets下的资源文件到SD卡中(可以超过1M)
- CD光盘音频cda文件拷贝翻录为wma文件
- java 拷贝文件和读取ini文件
- Android Studio APP 模块 与 lib 模块同时含有jniLibs 引发的so文件拷贝异常
- 文件拷贝
- 多线程递归拷贝多层目录文件
- java 文件夹拷贝(文件夹里包含文件和文件夹) 代码
- 程序文件直接拷贝到pda设备 (USB连接pc pda wince6.0 C#)
- linux自动拷贝文件,并恢复ORACLE数据库
- VMWare虚拟机 使用vmtools拷贝文件 临时文件问题
- 5.7文件拷贝
- 用shell脚本挂载linux主机拷贝相应文件copy.sh
- opensuse安装文件拷贝完成后无法进行图像界面安装
- scp拷贝文件报错-bash: scp: command not found
- 如何往Windows 2012 R2系统盘system32目录下面拷贝文件 推荐
- rman的conver方法拷贝ASM文件
- java文件的拷贝
- Linux下文件/文件夹 创建/拷贝/删除/剪切 小记
- python 拷贝特定后缀名文件,并保留原始目录结构的实例