In my program, the user can load a file with links (it’s a webcrawler), but I need to verify if the file that the user chooses is plain text or something else (only plain text will be allowed).
Is it possible to do this? If it’s useful, I’m using JFileChooser to open the file.
EDIT:
What is expected from the user: a text file containing URLs.
What I want to avoid: the user loads an MP3 file or a document from the MS Word (examples).
A file is just a series of bytes, and without further information, you cannot tell whether these bytes are supposed to be code points in some string encoding (say, ASCII or UTF-8 or ANSI-something) or something else. You will have to resort to heuristics, such as:
But here’s another solution: Just treat everything you receive as text, applying the necessary transformations where needed (e.g. HTML-encode when sending to a web browser). As long as you prevent the file from being interpreted as binary data (such as a user double-clicking the file), the worst you’ll produce is gibberish data.