• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading in a file  RSS feed

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to read in a file that will be be given at run time but everytime i run the program it hangs. I mean it runs and runs and runs but is doing nothing. Here is the code.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As written, if the first argument is "-i", then you'll be prompted for input; but if it's not, then the program will read a line from the console without prompting. Is that what you intend? Is it "hanging" just because it's waiting for that one line?

There can actually be some issues with data gettting "stuck" when you use BufferedReader with System.in. You always need to turn buffering off by using a 1-character buffer (I added text in bold):

BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What it is suppose to do is read input from a file. Not of the screen in -i is not there. I just can't get it to read it.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you don't open a file anywhere, do you? That would probably mean using a FileReader, and there's none to be found anywhere here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!