B
Bruce Lee
Is there any easy way to get Java to determine whether a file is a binary
file or plain text ascii file?
file or plain text ascii file?
Bruce Lee said:Is there any easy way to get Java to determine whether a file is a binary
file or plain text ascii file?
Matt Humphrey said:Files are simply sequences of (binary) bytes--there's no way to tell
whether it's supposed to contain only bytes that represent printable ascii
(or unicode) or any particular binary pattern. You can read the file to
find out--if you find values that signify unlikely or non-printable
characters you can deem the file binary or corrupt. Similarly, there are
heuristics (based on convention) for guessing the "type" of the file based
on the first few bytes, but there's no guarantee these are correct either.
(And files with 2-byte UNICODE characters can really confuse things.)
Of course, you could require that text files end in "txt" or
something--it's no worse than any of the above and significantly easier.
Matt Humphrey said:Files are simply sequences of (binary) bytes--there's no way to tell whether
it's supposed to contain only bytes that represent printable ascii (or
unicode) or any particular binary pattern. You can read the file to find
out--if you find values that signify unlikely or non-printable characters
you can deem the file binary or corrupt. Similarly, there are heuristics
(based on convention) for guessing the "type" of the file based on the first
few bytes, but there's no guarantee these are correct either. (And files
with 2-byte UNICODE characters can really confuse things.)
Of course, you could require that text files end in "txt" or something--it's
no worse than any of the above and significantly easier.
What are you trying to do?
Cheers,
Is there any easy way to get Java to determine whether a file is a binary
file or plain text ascii file?
Bruce Lee said:Also, are ASCII 7bit and binary 8bit or something?
Is there a way to find
this out - like analyse a byte?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.