Text files may contain 8-bit chars, 16-bit chars or even 32-bit chars encoded as 8-bit surrogates.
Lines are terminated by the misnamed line.separator string which you can discover with
There should be a terminator on the last line, even though system property name line.separator suggests there should not. Putting the terminator there will not be treated as a final empty line. Programs will usually still work if the final terminator is missing, but in earlier versions of Java readLine would not read the last line without it.
There is no mechanism to keep track of which encodingwas used to write a text file. You just have to know. The Encoding Recogniserwill help you guess. Originally this caused no problem, because people rarely exchanged files except with coworkers. Everyone’s files were encoded in the same local national encoding. Today people share files all over the globe. It is best to use ASCII (American Standard Code for Information Interchange) for 7-bit chars, ISO-8859-1 for 8-bit and UTF-8 for 16-bit.
The classes to write text files include File, FileOutputStream, OutputStreamWriter and BufferedWriter. The classes to read text files include File, FileInputStream, InputStreamReader and BufferedReader. The File I/O Amanuensis will generate Java code for you to read and write text files (as well as other types).
|
|
available on the web at: |
http://mindprod.com/jgloss/textfile.html |
optional Replicator mirror
|
J:\mindprod\jgloss\textfile.html | |
![]() |
Please email your
feedback for publication,
letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear
wording, broken/redirected link reports, suggestions to improve this page or comments to
Roedy Green :
| |
| Blog | Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[184.73.74.47] |
|
| Feedback | You are visitor number 11. | |