Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

does OS matter for ASCII file ?  RSS feed

 
Raj Bhandari
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A conceptual question.
If I want to read data from a ASCII text file, does it matter which OS the file was created on i.e. will there be problems with line termination, etc.
Also, when I read the file, what is advisable: open it in binary mode or text mode ? What is the default mode in java ?
Tks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a java.io.Reader (i.e., a FileReader) to read a file, then FileReader will take care of the differences in line endings for you. This is the equivalent of opening a file in "text mode" in C. Using a java.io.InputStream instead -- i.e., java.io.FileInputStream -- is the equivalent of "binary mode" in C, and you'll see platform-dependent line terminators.
 
Raj Bhandari
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest, this answers my question very well.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be careful with what you mean by "ASCII" file. Do you mean
real 7-bit ASCII, the Basic Latin block of the Unicode character set, or do
you mean more vaguely, text, in whatever the default encoding is for your
platform (usually Latin-1 in UNIX and cp1252 in Windows)?

FileReader assumes you want your platform's default; if you want to specify
true ASCII you have to pass the charset name to InputStreamReader.
Here are two examples that read a file line by line. The second uses
java.util.Scanner, which was introduced in the current version of Java.
It has additional features like parsing numbers and matching patterns.
 
Raj Bhandari
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff for the clarification.
At that point of time all I knew from the client was that they have an ascii file and I assumed it was a simple text file with standard ascii characters. Now I know that its created on a unix platform.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[EFH]: If you use a java.io.Reader (i.e., a FileReader) to read a file, then FileReader will take care of the differences in line endings for you.

Mmm, no, as far as I'm aware FileReader does not handle the differences in line terminators. It handles all the charset encoding issues, assuming the system default charset is what you really want (which often it's not). But differences in line separators are not included in this. For line separators, it's probably easiest to use a BufferedReader (pre-JDK 1.5) or a Scanner (JDK 1.5+) as Jeff showed above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!