博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
File类复制文件
阅读量:4652 次
发布时间:2019-06-09

本文共 1503 字,大约阅读时间需要 5 分钟。

import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;public class CreatDirectory{ 	public static void main(String[] args) throws Exception  	{ 		Copy(args); 		 	} 	public static void Copy(String args[]) throws Exception 	{ 		File SourcePath=null;		File TargetPath=null; 		if(args.length==2) 		{ 			SourcePath=new File(args[0]); 			TargetPath=new File(args[1]); 			if(SourcePath.getParentFile()==null) 			{ 				System.out.println("暂不支持整个分区的文件拷贝!"); 			} 			else if(SourcePath.exists()) 			{  					if(TargetPath.getName().indexOf(".")==-1)			  		{			  					  				TargetPath.mkdirs();			  			File tempFile=new File(TargetPath.getPath()+File.separator+SourcePath.getName());			  			readWriteFile(SourcePath,tempFile);			  		}			  		else			  		{		  				String s=TargetPath.getParent();		  				(new File(s)).mkdirs();								readWriteFile(SourcePath,TargetPath);			  			}				 			} 			else 			{ 				System.out.println("源文件不存在!"); 			} 			 		} 		else 		{ 			System.out.println("您输入命令格式不正确!\n\t例如:java CreatDirectory c:\\123.txt d:\\"); 		} 	} 	 	public static void readWriteFile(File sourceFile,File targeFile) throws Exception 	{ 		InputStream input=new FileInputStream(sourceFile); 		OutputStream output=new FileOutputStream(targeFile); 		 		int temp=0; 		while((temp=input.read())!=-1) 		{ 			output.write(temp); 		} 		input.close(); 		output.close(); 	}}

 

转载于:https://www.cnblogs.com/xiongyu/archive/2011/11/08/2241201.html

你可能感兴趣的文章
Sublime Text 3 及Package Control 安装(附上一个3103可用的Key)
查看>>
基于uFUN开发板的心率计(一)DMA方式获取传感器数据
查看>>
【dp】船
查看>>
oracle, group by, having, where
查看>>
⑥python模块初识、pyc和PyCodeObject
查看>>
nodejs pm2使用
查看>>
CSS选择器总结
查看>>
mysql中sql语句
查看>>
sql语句的各种模糊查询语句
查看>>
C#操作OFFICE一(EXCEL)
查看>>
【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
查看>>
移动端单屏解决方案
查看>>
web渗透测试基本步骤
查看>>
使用Struts2标签遍历集合
查看>>
angular.isUndefined()
查看>>
第一次软件工程作业(改进版)
查看>>
网络流24题-飞行员配对方案问题
查看>>
Jenkins 2.16.3默认没有Launch agent via Java Web Start,如何配置使用
查看>>
引入css的四种方式
查看>>
iOS开发UI篇—transframe属性(形变)
查看>>