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:
optional Replicator mirror
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 : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[18.104.22.168]
|Feedback||You are visitor number 11.|