Am trying to rename all the files in a given directory,for ex if theres a file called hello.xls,hello1.xls then after the execution of the program it must become hello_Bs.xls,hello1_Bs.xls
I have written a simple code for this but am getting null pointer exception.
can anybody pls temme wats worng with the code.
import java.io.*;
import java.lang.*;
import java.util.*;
public class Ls {
public static void main(String args[])
{
String[] dir = new java.io.File("c:\\Documents and Settings\\Admin\\Desktop\\New Folder").list();
java.util.Arrays.sort(dir);
File f[] = new File("c:\\Documents and Settings\\Admin\\Desktop\\New Folder)").listFiles();
int len1 = dir.length;
for (int i=0;i<len1; i++)
{
System.out.println(dir[i]);
String name = f[i].getName();
System.out.println(name);
int j=name.indexOf('.');
System.out.println(j);
String newname = name.substring(0,j-1) + "_BS" + name.substring(j+1,name.length());
System.out.println(newname);
File newFileName=new File(f[i].getParentFile(), newname);
System.out.println("newFileName="+newFileName);
f[i].renameTo(newFileName);
}
}
}


Sign In
Create Account

Guest_anand.sundaramurthy_*
Back to top










