将不同目录下的文件,复制到同一个目录下。

    技术2022-05-11  25

    import java.io.*; public class FileCopy {     public  void copyfile(String origpath,String name, String nowpath) throws IOException //使用FileInputStream和FileOutStream     {                 File nowcontent = new File(nowpath);                 if(!nowcontent.exists())         {             nowcontent.mkdirs();         }         nowcontent = null;         File origFile = new File(origpath,name);                 File nowFile = new File(nowpath,name);                 FileInputStream fi = new FileInputStream(origFile);         FileOutputStream fo = new FileOutputStream(nowFile);         byte data[] = new byte[2048];         int len = 0;   while((len=fi.read(data))>0)         fo.write(data,0,len);         fi.close();         fo.close();         nowFile = null;         origFile = null;    }         public static void main(String[] args)     {         try         {             FileCopy fc = new FileCopy();             fc.copyfile("C://","Hello.java","F://abc");             fc.copyfile("d://","ad.txt","F://abc");         }         catch (IOException e)         {             e.printStackTrace();         }     } }

    最新回复(0)