Random Java questions
Hi. I have a couple of questions about Java.
(1) How do I read a file into a string? Seems to be something pretty simple to do, but I can't find out how to do it. The routine should ideally auto-detect if the file is in UTF-8 or UTF-16 format and act accordingly.
(2) TreeMap and Hashtable appear to expect you to create object instances for every key. What if you just want to use integers as keys? Isn't it wasteful to create loads of Integer object instances for that?
Thanks a lot for any reply.
EDIT: The best way I could find to do (1) is to do this: (a) start a FileInputStream on the file, (b) read the first two bytes, (c) see if they indicate UTF-16LE or UTF-16BE and create an InputStreamReader accordingly; (d) if they don't, read another byte and see if the three bytes make a UTF-8 BOM, if so, create an InputStreamReader for that; (e) otherwise, since FileInputStream is braindead and doesn't have a seek(), close() the file and create a new FileInputStream, and then an InputStreamReader for the default character encoding; (f) then create a BufferedReader using whatever InputStreamReader you've created above. Furthermore, since FileInputStream is so useless that it doesn't even have an eof(), you have to tinker with IOExceptions to handle the special cases of files that are shorter than three bytes.
If you ask me, I think that is utterly uselessly and unnecessarily complicated for a task as basic as this. Either way, though, thanks a lot for all the helpful advise you gave. What would I do without you lot :-)
(1) How do I read a file into a string? Seems to be something pretty simple to do, but I can't find out how to do it. The routine should ideally auto-detect if the file is in UTF-8 or UTF-16 format and act accordingly.
(2) TreeMap and Hashtable appear to expect you to create object instances for every key. What if you just want to use integers as keys? Isn't it wasteful to create loads of Integer object instances for that?
Thanks a lot for any reply.
EDIT: The best way I could find to do (1) is to do this: (a) start a FileInputStream on the file, (b) read the first two bytes, (c) see if they indicate UTF-16LE or UTF-16BE and create an InputStreamReader accordingly; (d) if they don't, read another byte and see if the three bytes make a UTF-8 BOM, if so, create an InputStreamReader for that; (e) otherwise, since FileInputStream is braindead and doesn't have a seek(), close() the file and create a new FileInputStream, and then an InputStreamReader for the default character encoding; (f) then create a BufferedReader using whatever InputStreamReader you've created above. Furthermore, since FileInputStream is so useless that it doesn't even have an eof(), you have to tinker with IOExceptions to handle the special cases of files that are shorter than three bytes.
If you ask me, I think that is utterly uselessly and unnecessarily complicated for a task as basic as this. Either way, though, thanks a lot for all the helpful advise you gave. What would I do without you lot :-)
