你可以使用java.io.File
public boolean renameTo(File dest)方法应该基本上可以解决了,不过这个函数是move还是copy的.

解决方案 »

  1.   

    import java.io.*;public class mytest0 {
    static int filecopycount=1;
    public mytest0() {}public static void main(String[] args) throws FileNotFoundException,IOException,Exception {//接收参数并进行相应操作
    System.out.print("input:");
    DataInputStream ds=new DataInputStream(System.in);
    String str=ds.readLine();
    if(str.equals("copy"))
      copyfile(args[0],args[1]);
    else if(str.equals("delete"))
      deletefile(args[0]);
    else if(str.equals("write"))
      writefile(args[0]);
    else if(str.equals("list"))
      list(args[0]);
    else
      System.err.println("Input error!");
    ds.close();
    }private static void writefile(String str) throws IOException {//写入文件
      OutputStream os=new FileOutputStream("xiruo_file.txt");
      PrintStream ps=new PrintStream(os);
      ps.println(str);
      ps.close();
      os.close();
      System.exit(0);
    }private static void copyfile(String str,String str1) throws FileNotFoundException {//拷贝文件或文件夹
    try {
    File file=new File(str);
    InputStream is=null;
    OutputStream os=null;
    byte[] b;
    File f=new File(str1);
    if(!file.isDirectory()) {
      is=new FileInputStream(str);
      b=new byte[is.available()];
      is.read(b);
      os=new FileOutputStream(str1);
      os.write(b);
      is.close();
      os.close();
      return;
    } else if(!f.exists())
      f.mkdirs();
    File[] filename=file.listFiles();
    for(int i=0;i<filename.length;i++) {
        copyfile(filename[i].getAbsolutePath(),str1+"/"+filename[i].getName());
    }
    } catch(IOException ex) {
    filecopycount++;
    System.out.println("filecopycount:"+String.valueOf(filecopycount));
    if(filecopycount<=5)
    copyfile(str,str1);
    System.err.println("err:"+ex.toString());
    }
    }private static void deletefile(String delpath) throws FileNotFoundException,IOException {//删除文件或文件夹
    File file=new File(delpath);
    if(!file.isDirectory()) {
      file.delete();
      return;
    } else if(file.isDirectory()) {
      String[] filelist=file.list();
      for(int i=0;i<filelist.length;i++) {
        File delfile=new File(delpath+"/"+filelist[i]);
        if(!delfile.isDirectory())
          delfile.delete();
        else if(delfile.isDirectory())
          deletefile(filelist[i]);
      }
      file.delete();
    }
    System.exit(0);
    }private static void list(String listPath) throws FileNotFoundException,IOException {//列出一个目录下的所有文件和文件夹
      File file=new File(listPath);
      String[] filelist;
      if(!file.isDirectory()) {
        System.out.println("<dir>"+file.getName());
        return;
      } else if(file.isDirectory()) {
        filelist=file.list();
        for(int i=0;i<filelist.length;i++) {
          File listfile=new File(listPath+"/"+filelist[i]);
          if(!listfile.isDirectory())
            System.out.println(listfile.getName());
          else if(listfile.isDirectory())
            list(filelist[i]);
        }
    }
    }
    }
      

  2.   

    把main方法去掉,加入package包,然后在classpath里加入该包的路径,编译之后就成为可以被其他类调用的bean了
    我要分