Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check whether a file is ascii or binary

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!

I wrote a tiny webbased file browser (jsp) that can show the content of a folder structure and will allow to view ascii files in the browser. But how can I check if the selected file is an ascii file or a binary file? Found no method in class "File" for this task. :-(

Any ideas?

Cheers

frag

patrick.metz@epost.de
 
Hm... I missed to mention that I am in a solaris environment and solaris doesn't care about file extensions. So I can be possible to have a plain text file without extensions like .txt, .log and so on.

patrick.metz@epost.de
 
There is "javax.activation.FileTypeMap" (part of J2EE), but it's an abstract class. If you can find (or write one yourself) an implementation for Solaris ...

"The FileTypeMap is an abstract class that provides a data typing interface for files. Implementations of this class will implement the getContentType methods which will derive a content type from a file name or a File object. FileTypeMaps could use any scheme to determine the data type, from examining the file extension of a file (like the MimetypesFileTypeMap) to opening the file and trying to derive its type from the contents of the file. The FileDataSource class uses the default FileTypeMap (a MimetypesFileTypeMap unless changed) to determine the content type of files."

I know nothing about Solaris filesystem, but according to the following, the file type could be coded in the first bytes of the file... ?
Found at : :
Most file types have a consistent 'header' in the first few bytes. For example, look at a dump of a GIF file. It always starts with 'GIF87':

od -c sample.gif | head -1
0000000 G I F 8 7 a 273 \0 273 \0 367 9 \0 275 336 377

So the "magic number" for a GIF file is: GIF87.
Magic numbers are listing in the file /etc/magic (see man magic for a detailed explanation). The 'file' command uses magic numbers to try and figure out what file type a file is.

Another possibility is to use "java.lang.Runtime" to execute a native cmd to determine the file type. See thread269-611384
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top