zoukankan      html  css  js  c++  java
  • File类复制文件

    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();
     	}
    }
    
  • 相关阅读:
    最精简的django程序
    spring+mongo
    从零搭建mongo分片集群的简洁方法
    java对redis的基本操作
    awk输出指定列
    sed输出指定行
    Bash的循环结构(for和while)
    用ffmpeg切割音频文件
    Python判断字符串是否全是字母或数字
    Python函数: any()和all()的用法
  • 原文地址:https://www.cnblogs.com/xiongyu/p/2241201.html
Copyright © 2011-2022 走看看