将文本文件从一个目录移动到另一个目录 [英] moving text file from one directory to another

查看:125
本文介绍了将文本文件从一个目录移动到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Java程序将文本文件从一个目录移动到另一个目录的问题

程式:

Problem in moving a text file to one directory to another directory through java program

program:

import java.io.File;
	// Move File, Directory to another Directory
public class moving {
	public static void main(String args[])throws Exception
		{

        // enter first the names of files as command line argument , first write the oldfile name and then the new directory name e.g.
			// java RenameFileDir oldfile.txt newdir
        
		File file = new File(args[0]);
			File dir = new File("C:\" "+args[1]);  
		// destination directory
		
		//The File constructor Creates a new File instance from a parent abstract pathname and a child pathname string.
		File f = new File(dir, file.getName());
		
		boolean move= file.renameTo(f); 
		 if (move)
		 {
             System.out.println("File was successfully moved.\n");
         } 
		 else 
		 {
             System.out.println("File was not successfully moved.\n");
         }


    }
}



错误:



ERROR:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at moving.main(moving.java:10)




请任何人帮助解决此问题.




Please anybody help to solve this problem.

推荐答案

如果此代码中出现异常,则问题在于命令行:您未提供参数,因此args[1]不提供参数存在.在执行此类操作之前,您需要检查此数组的长度.

请下次,如果您有来自异常堆栈的第10行的信息,请在您发布的源代码中标记此行,不要强迫人们猜测.

—SA
If the exception in this code, the problem is command line: you did not supply parameters, so args[1] does not exist. You need to check up the length of this array before doing such things.

Please, next time, if your have information from the exception stack for the line 10, mark this line in your source code you post, don''t force people into guessing.

—SA


此链接可以帮助您实现类似的示例.
Java-将一个文件复制到另一个文件 [
This link may help you similar example that you implemented.
Java - Copying one file to another[^]


这篇关于将文本文件从一个目录移动到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆