Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

does OS matter for ASCII file ?

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest, this answers my question very well.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic