Home arrow static arrow Java Programming [Archive] - how do I get this to return a file extention
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how do I get this to return a file extention
This topic has 17 replies on 2 pages.    1 | 2 | Next »

Posts:31
Registered: 6/21/04
how do I get this to return a file extention  
Jun 23, 2004 8:59 AM



 
import java.io.File;
import java.io.IOException;
import java.util.Date;

public class Length {

public static void main(String[] args) throws IOException{
//Lookig at blah.txt or if its not there make one
File myFile = new File("C:
Documents and Settings\\adam
blah.txt");
if(!myFile.exists()){
myFile.createNewFile();
}
//Printing out Length, lastModified, and returning path to the file
System.out.println("File length - "+ myFile.length());
System.out.println( "File last modified - "+ new Date(myFile.lastModified()).toLocaleString() );
System.out.println("Path to file - " + myFile.getAbsolutePath());

}
}

What should I use to return the file extention
 

Posts:6,750
Registered: 1/25/04
Re: how do I get this to return a file extention  
Jun 23, 2004 9:15 AM (reply 1 of 17)



 
I would use String.lastIndexOf to find the last "." and get everything after that.
 

Posts:31
Registered: 6/21/04
Re: how do I get this to return a file extention  
Jun 23, 2004 11:26 AM (reply 2 of 17)



 
I tried that doesnt seen to work, it looks at the path name but if the extention is not in the path it will not show it.
 

Posts:11,200
Registered: 7/22/99
Re: how do I get this to return a file extention  
Jun 23, 2004 11:52 AM (reply 3 of 17)



 
If the file name extension is not a part of the file's name, what is it?
 

Posts:31
Registered: 6/21/04
Re: how do I get this to return a file extention  
Jun 23, 2004 11:53 AM (reply 4 of 17)



 
It is not in the pathname
 

Posts:31
Registered: 6/21/04
Re: how do I get this to return a file extention  
Jun 23, 2004 11:53 AM (reply 5 of 17)



 
Like this

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.lang.StringBuffer;

public class Length {

public static void main(String[] args) throws IOException{
//Lookig at blah.txt or if its not there make one
File myFile = new File("C:
Documents and Settings\\adam
New Bitmap Image");
if(!myFile.exists()){
myFile.createNewFile();
}
//Printing out Length, lastModified, and returning path to the file
System.out.println("File length - "+ myFile.length());
System.out.println( "File last modified - "+ new Date(myFile.lastModified()).toLocaleString() );
System.out.println("Path to file - " + myFile.getAbsolutePath());

}
}

 

Posts:11,200
Registered: 7/22/99
Re: how do I get this to return a file extention  
Jun 23, 2004 11:54 AM (reply 6 of 17)



 
What is "pathname"?
 

Posts:6,750
Registered: 1/25/04
Re: how do I get this to return a file extention  
Jun 23, 2004 11:55 AM (reply 7 of 17)



 
You do know that the file C:
Documents and Settings\\adam
New Bitmap Image has no extension, right?
 

Posts:31
Registered: 6/21/04
Re: how do I get this to return a file extention  
Jun 23, 2004 11:57 AM (reply 8 of 17)



 
.bmp
 

Posts:11,200
Registered: 7/22/99
Re: how do I get this to return a file extention  
Jun 23, 2004 11:59 AM (reply 9 of 17)



 
File myFile = new File("C:
Documents and Settings\\adam
New Bitmap Image");
if(!myFile.exists()){
myFile.createNewFile();
}
What should the file name extension of that file be?

Even if you restrict to bitmap image files, there are several file formats you can choose from. BMP is a popular one, and so are XPM, GIF, PPM, and PCX, to name a few.

And that does not rule out the possibility that there could be a text file called "New Bitmap Image."
 

Posts:6,750
Registered: 1/25/04
Re: how do I get this to return a file extention  
Jun 23, 2004 12:01 PM (reply 10 of 17)



 
.bmp

No, it isn't .bmp, even if it's a bmp file. The extension of a file is strictly part of its name, and the name you gave has no extension. If you rename it to "New Bitmap Image.bmp" then it should work fine.
 

Posts:31
Registered: 6/21/04
Re: how do I get this to return a file extention  
Jun 23, 2004 12:03 PM (reply 11 of 17)



 
I would have to put the extention in the pathname, but is there a way to get it iof it is not in the pathname.
 

Posts:11,200
Registered: 7/22/99
Re: how do I get this to return a file extention  
Jun 23, 2004 12:05 PM (reply 12 of 17)



 
The extension is always a part of the path name. Microsoft Windows Explorer tries its best to hide this simple fact from the confused user (and causing more confusion in the process) but the extension is still a part of the file name.
 

Posts:6,750
Registered: 1/25/04
Re: how do I get this to return a file extention  
Jun 23, 2004 12:20 PM (reply 13 of 17)



 
And if you do have a file with no extension, such as "c:\NewFile" then the only way to figure out what the extension should be is to read the file header, and I don't even know if that would work.
 

Posts:31
Registered: 6/21/04
Re: how do I get this to return a file extention  
Jun 23, 2004 12:28 PM (reply 14 of 17)



 
How whould I get in the header and check. Is there an api that does that?
 
This topic has 17 replies on 2 pages.    1 | 2 | Next »