• 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

reading a file in Java

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I am trying to read a ASCII raw file in as a String in java and then turning into a char array.
So for example it should be able to read "ab" since ASCII values for those are 96 and 97. However,
I want the program to end if in the file has an ASCII value that is not 32 - 126.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maria, welcome to the Ranch!

I was going to ask you to explain what your problem was, but then I noticed that you were checking each character and rejecting it if it was less than 32 and greater than 126.

By the way the Ranch Office is for discussing the Ranch itself, not for discussing Java programming problems. So I'll just move this thread to the Beginning Java forum.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again
Don't use += on Strings inside a loop because you may get poor performance. Add your lines to a List or append them to a StringBuilderDon't use System.exit. If you have problems, throw an Exception instead. System.exit is potentially harmful in a multi‑threaded environment because it can stop the JVM while some tasks are only partially done.
There is another serious error: you are not closing the file. You should do that by means of try with resources. Otherwise there is a risk of your file being locked forever.
Don't test for inputFile != null. You cannot have inputFile equal to null, so you can delete that part.
Why are you using a char[]? That is low‑level code, which you should avoid in a high‑level language. Where did you get the numbers 32 and 126 from? Remind yourself of the range of ASCII which goes 0...127 (=7f). You obviously wanted ASCII minus the control characters and “del”. There is a slight hazard to doing that; if you write your file on a Windows® box, it will use \r\n as line end (=0x000d0x0000a in hexadecimal), but if you read that file on a Mac or Linux box, that will use \n as line end, so there is a risk that the \r characters will be read using nextLine() and will cause a spurious error.
There is no need for casts since chars are not letters but numbers. Don't write the decimal numbers. Write
if (letters[i] < ' ' || letters[i] > '~')...
Don't wait until you have read the whole file before you test for wrong characters. Do it inside the loop, so you can stop reading before you have gone through the whole file. You can also give some hint where the error occurs:-Anyway. there is bound to be a better way to do it. One way to do it is to create a regular expression (=regex) which represents ASCII characters minus control characters, but including line end, tab, etc. There is a method in the String class which tests whether it matches a regex.
 
reply
    Bookmark Topic Watch Topic
  • New Topic