【发布时间】:2013-04-06 12:31:55
【问题描述】:
我一直在尝试制作一个程序,将多个 .mp4 文件重命名为其所在文件夹的名称。该程序有时可以处理几个文件,但最终会引发空指针异常
我尝试了多种不同的方法,但似乎都无法正常工作,而且我对 windows 7 相关的 java 不是很熟悉。
任何人都可以看到问题吗?干杯。
public static void main (String []args) throws InterruptedException
{
String dir = "D:\\New folder";
File directory = new File(dir);
File[] files = directory.listFiles();
File tempd;
File[] tempf;
String temps;
int filecount = 0;
for (int index = 0; index < files.length; index++)
{
temps = files[index].toString();
tempd = new File(temps);
tempf = tempd.listFiles();
for (int i = 0; i < tempf.length; i++)
{
String[] tempsRel = temps.split("\\\\");
if (tempf[i].toString().endsWith("mp4"))
{
boolean success = tempf[i].renameTo(new File(dir + "\\" + tempsRel[tempsRel.length-1] + ".mp4"));
if (success)
{
System.out.println("RENAMED FILE ==> " + tempsRel[tempsRel.length-1] + ".mp4");
}}}}
System.exit(0);
}
【问题讨论】:
-
请发送堆栈跟踪。
标签: java windows file nullpointerexception renaming