Get the file extension in Java
yuri648
Posted messages
785
Status
Member
-
KX Posted messages 19031 Status Moderator -
KX Posted messages 19031 Status Moderator -
Hello,
how to obtain a file extension in Java or what method can do that?
and thanks in advance
how to obtain a file extension in Java or what method can do that?
and thanks in advance
Configuration: Windows XP Firefox 2.0.0.20
3 answers
-
Hello Yuri,
There is no method to do that (in general an extension is useless).
If you want to retrieve the extension yourself, you need to use the split(".") method of the String class. You retrieve the last entry of the returned array and that’s it!
Best regards,
--
rtfm and jfgi
Simple "thank you" makes helping people happy :) -
"chemin" could be just the file name:
String chemin = "mon/chemin/de/fichier/Fichier.toto.txt";
String ext = chemin.substring(chemin.lastIndexOf(".")); //gets the extension
System.out.println(ext); //-> .txt-
Attention: you should handle the case where there is no extension (not mandatory). In particular, be wary of paths containing a dot in the folder name or hidden files under Linux whose names start with a dot (but which are not the extension!). Here are some examples of paths and the expected result: "C:\test\test.txt" or "/test/test.txt" --> "txt" "C:\test\test" or "/test/test" --> "" "C:\test.test\test" or "/test.test/test" --> "" "C:\test\.test" or "/test/.test" --> ""
-
-
I have another problem, please help STP. I have this error:
Exception in thread "main" java.lang.NullPointerException
at pdf.<init>(pdf.java:30)
at nn.main(nn.java:11)</init>
here's the pdf class:
import java.io.File;
import java.util.Stack;
public class pdf {
public pdf()
{
Stack<String> pile=new Stack();
String dd="d:/";
String v;
int i;
boolean bol=false;
String h="pdf";
File[] lis=null;
i=0;
while(bol==false)
{
// dd=(String) pile.pop();
File g=new File(dd);
lis=g.listFiles();
i=0;
while(i<lis.length )
{
if(lis[i].isDirectory()==true){pile.push(lis[i].getPath()); }
else {
v=getExtension(lis[i]);
if(h.equals(v)){System.out.println("le fichier"+ lis[i]);}
}
i=i+1;
}
if(!pile.isEmpty()) {dd=(String) pile.pop(); }
else{bol=true;}
}
public static String getExtension(File f) {
if(f != null) {
String filename = f.getName();
int i = filename.lastIndexOf('.');
if(i>0 && i<filename.length()-1) {
return filename.substring(i+1).toLowerCase();
}
}
return null;
}
}
and the main class:
import java.io.IOException;
public class nn {
public static void main (String arguments[]) throws IOException
{
pdf mm=new pdf();
}
}
and thanks in advance.-
Hello Yuri648,
First, it’s nice to see that after you explained that you should use either an ArrayDeque or a LinkedList to implement stacks, you’re using a Stack. Anyway, do as you please...
Can you tell me on which line this exception is thrown please (put it in bold), and repost your message with indentation and using the CODE tag please ?
Thanks,
--
rtfm and jfgi
Simple "thank you" makes helping people happy :) -
-
-
-
You have a wonderful tool in Eclipse: the debugger (it's simply amazing). To detect where your problem comes from, you put a breakpoint on this line: while(i<lis.length). Then you run the execution in debug mode (the little beetle or F11), and your program will stop at every pass through this line (so at every loop iteration), and will show you the state of the variables at that moment. You will therefore be able to find the exact moment when your program crashes (by looking at the value of i...). Understand that I have enough difficulty helping you because I don't have the same filesystem as you (and on my side, with c:/, the program runs without a problem). Best regards, -- rtfm and jfgi Simple "thank you" makes helping people happy :)
-