原文:java文件操作之移动文件到指定的目录
代码下载地址:http://www.zuidaima.com/share/1550463261166592.htm
java文件操作之移动文件到指定的目录,通过renameTo方法实现,而非copy后delete
class="java" name="code">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(); } } }