• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Know ascii file or binary file beforehand

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Is it possible for me to know whether a file is an ascii file or a binary file using any Java method.
My intention is to display the contents of the file if the file is a text file (whether it is .txt, .java, .html etc) and not do anything if it is a binary file (.bmp, .gif, .class, .doc etc.)
Thank you
Sathish
 
SathishVJ
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to be posting a reply to my own question. But I found this answer to the question at a different site. I guess somebody could corroborate this or give me another answer.
{
There is no easy way to tell since ASCII characters are binary. Depending upon how you read the file determines how you treat the data. For instance, the text "CAFE" if read as an int is the value 1128351301. Since Java also deals with Unicode and not just ASCII, you get to worry about 8-bit vs. 16-bit characters, too.
}
Thanks
Sathish
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing this Satish, but pleas note that The Java Ranch has a naming policy, described here and "SathishVJ" is not a valid name. Please choose one which meets the requirements.
Thanks.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
well if you'r file has an extension then u can use file filter to filter the files as u need.list() and listFiles() methods are oevrloaded to versions which can filter the files using .extensions.is using list() extend from FilenameFilter else if using listfile() extend from FileFilter.right these both are interfaces with an abstract method accept().list and listFile() call this accept () method.hope you're doubt is cleared if not please feel free to ask me again.
bye
anuj khanna
denedal@hotmail.com
 
And then the flying monkeys attacked. My only defense was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic