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.
Reading a file
- 8
Locked
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...
+2
Justin Smith
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.
0
ImDevin
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! :)
0
ayhem bouabid
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...
0
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...
+3
Petros
4 August 2020, 16:46
Tough one. The IOException was throwing me off.
0
Lucas Hoage
10 June 2020, 16:56
That was a cool exercise. I really enjoyed that.
0
Andriy
3 September 2019, 02:16
Validation says : "The program displays too few lines." What is wrong ?
0
senthamil
27 June 2019, 13:11
what should i give here ?
0