So I renamed files or changed their extension.
public static void modify(File file) { int index = file.getName().lastIndexOf(".");
edit: the John method renames the file (while preserving the extension). To change the extension, do:
public static File changeExtension(File f, String newExtension) { int i = f.getName().lastIndexOf('.'); String name = f.getName().substring(0,i); return new File(f.getParent() + "/" + name + newExtension); }
This only changes the last extension to the file name, i.e. the .gz part of archive.tar.gz . Therefore, it works great with hidden Linux files, for which the name begins with . This is completely safe, because if getParent() returns null (that is, getParent() when the parent is the system root), it is "cast" to an empty string, since the entire argument to the File constructor is evaluated first.
The only time you get funny output is if you pass a File representing the system root itself, in which case null will be added before the rest of the path line.
source share