Reading a file

  • 8
  • Locked
We've received a new task from Captain Squirrels: read a file name from the console. And then display the contents of the file in the console (on the screen). Don't forget to free up resources by closing the streams for file input and keyboard input.
You can't complete this task, because you're not signed in.
Comments (9)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Karas Java Developer
28 April 2022, 00:11
Nothing to hide here, read a line from the console, if you are at this pont you remember our friend BufferedReader with System.in to prompt for the title of the file. Store it as a string or whatever you want. Pass it as the name for a FileInputStream Do another Bufferred Reader but instead of System.in pass the the file from prior step. Do a while loop with .readLine() on the file. System.out.println() inside the loop. Presto! Hopefully I am not giving too much...
Justin Smith
Level 38 , Greenfield, USA, United States
2 August 2021, 16:00
The task conditions are insufficient. Are we supposed to print the file contents all one one line or on multiple lines? If it has to be on multiple lines, by what criteria? Should we search for spaces to break it into multiple lines? The task conditions say nothing about this, but validation error "too few lines" suggests there is a hidden condition here they forgot to list.
ImDevin
Level 15 , Old Town, United States
2 June 2021, 04:05
the task itself wasn't too bad, but had to spend a ton of time reading/studying, both the Professor's stuff and googling, to get the hang of the InputStream. But, it was worth it. I think I'm getting it. Happy coding! :)
ayhem bouabid
Level 25 , Tunis, Tunisia
11 January 2021, 14:24
I liked the fact that copying and pasting will not solve this one. I had to make my own changes...
Ian De Bie Full Stack Developer
29 August 2020, 01:11
i needed to change two things to pass. i had to use print instead of println and cast my data to char in the print method. but i don't know the reason why that was necessary, lol...
Petros
Level 23 , Columbia, United States
4 August 2020, 16:46
Tough one. The IOException was throwing me off.
Lucas Hoage
Level 14 , Savannah, United States
10 June 2020, 16:56
That was a cool exercise. I really enjoyed that.
Andriy
Level 22 , Toronto, Canada
3 September 2019, 02:16
Validation says : "The program displays too few lines." What is wrong ?
senthamil
Level 18 , Korampallam, India
27 June 2019, 13:11
what should i give here ?