Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read ASCII char (0-31) from file?  RSS feed

 
J Yau
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read some character (e.g. "^[", "^M", "^L") from a file on Unix platform. However, all those character disappered in the BufferedReader stated in the following code
_____________________________________________________________
BufferedReader in = new BufferedReader(new FileReader(file));
String bufferLine;
while ((bufferLine = in.readLine()) != null) {
System.out.println(bufferLine);
}
in.close();
_____________________________________________________________
How can I successfully read the char. from the file?
 
Peter Storch
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The *Reader classes are already performing character conversions to unicode.
Try to use the *InputStream classes and use the byte[] arrays directly.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those chars do have valid Unicode values - a Reader should convert them successfully.
However, the readLine() method is probably considering the ^M (carrieage return) to be part of the line separator. See the docs for readLine() - \n, \r\n, and \n\r are all considered line separators, and so they will not appear as part of the String returned by readLine().
As for ^[ (escape) and ^L (form feed), I'm not sure. It's possible that you're not seeing them because they're not printable characters, and your platform doesn't know how to display them. Neither is in common use nowadays. Or, if you were to do a trim() (not shown in your code, but it's a common choice) then those control characters would be trimmed off if they're at the beginning or end of a string.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!