Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Know ascii file or binary file beforehand  RSS feed

 
SathishVJ
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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.
 
anuj khanna
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!