1. 程式人生 > >File類中移動檔案

File類中移動檔案

  • package com.zuidaima.util.file;
    
    import java.io.File;
    
    /**
     * 移動檔案到指定的目錄
     * 
     * @author www.zuidaima.com
     * 
     */
    public class MoveFile {
    
    	public static void main(String[] args) {
    		try {
    			File afile = new File("C:\\zuidaima_com_a\\zuidaima_com.txt");
    			if (afile.renameTo(new File("C:\\zuidaima_com_b\\" + afile.getName()))) {
    				System.out.println("File is moved successful!");
    			} else {
    				System.out.println("File is failed to move!");
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }