• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find a file is of binary type or not

 
Bill Thompson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Friends,

Is there any way that we can find whether a file is of binary type or not.

Thanks in adv,
Tn
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Bill Tn",
Please check your private messages.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the operating systems that I know of, binary-ness is not an attribute of the actual file. However, certain types of content are considered binary.

To find out the content type of an arbitrary file, there are two main approaches: -

  • Look at the file name - particularly the extension. A *.txt file is probably non-binary, while a *.zip file is probably binary.
  • Peek at the contents of the file, particularly any initial "magic number". For instance, a PostScript file (actually could be binary or text) starts "%!PS-Adobe" while a Java class starts 0xCAFEBABE (ho ho).


  • In Java, there are methods to guess the content type using each of these approaches. For some reason, they're in the URLConnection class. Look at guessContentTypeFromName() and guessContentTypeFromStream().
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!