This is a discussion on How can I rename all files in a folder using Java? within the Java Programming forums, part of the Software Development category; Hi All, Am trying to rename all the files in a given directory,for ex if theres a file called ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi All, 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. 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);
}
}
} |
| Sponsored Links |
| |||
| The problem is here... Code: String newname = name.substring(0,j-1) + "_BS" + name.substring(j+1,name.length()); Fixed code is... Code: import java.io.File;
public class Ls {
public static void main(String args[])
{
String[] dir = new java.io.File("test").list();
java.util.Arrays.sort(dir);
File f[] = new File("test").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) + "_BS" + name.substring(j,name.length());
System.out.println(newname);
File newFileName=new File(f[i].getParentFile(), newname);
System.out.println("newFileName="+newFileName);
f[i].renameTo(newFileName);
}
}
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hi, I tried this program but I am getting some problem in calling the "i" string. Could you please specify what "i" string is doing. Thanks a lot.
__________________ cheers Aman |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| create log files in java? | saravanan | Java Programming | 2 | 08-06-2008 03:43 AM |
| To rename user created objects in sql server 2005 | vadivelanshanmugam | Database Support | 3 | 03-06-2008 09:30 PM |
| Help me to rename the flex project | seesamjagan | Adobe Flex Programming | 0 | 01-03-2008 10:55 PM |
| Rename Files in Bulk in Windows XP | srikumar_l | Operating Systems | 0 | 12-19-2007 09:42 PM |
| How do I rename all the files from .htm to .html after copying them from a PC to a UN | oxygen | HTML, CSS and Javascript Coding Techniques | 1 | 07-27-2007 04:04 AM |