CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Andriy
Level 22
Toronto
  • 03.09.2019
  • 1072views
  • 2comments

"The program displays too few lines." What is wrong?

Question about the task Reading a file
Java Core,  Level 3,  Lesson 11
Archived

1. Read a file name from the console.
2. Display the contents of the file in the console (on the screen).
3. Don't forget to free up resources. Close the streams for file input and keyboard input.

Requirements:
  • The program must read the file name from the console.
  • The program must display the contents of the file.
  • You must close the file input stream (FileInputStream).
  • The BufferedReader must also be closed.
package com.codegym.task.task13.task1318; import java.io.*; import java.util.Scanner; /* Reading a file 1. Read a file name from the console. 2. Display the contents of the file in the console (on the screen). 3. Don't forget to free up resources. Close the streams for file input and keyboard input. Requirements: 1. The program must read the file name from the console. 2. The program must display the contents of the file. 3. You must close the file input stream (FileInputStream). 4. The BufferedReader must also be closed. */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader rd = null; InputStream inputStream = null; BufferedInputStream buffer = null; try { rd = new BufferedReader(new InputStreamReader(System.in)); String fileName = rd.readLine(); inputStream = new FileInputStream("/home/lap/Nextcloud/Java_2019/CodeGym/InputOutput/" + fileName + ""); buffer = new BufferedInputStream(inputStream); while (buffer.available() > 0) { char c = (char) buffer.read(); System.out.print(c); } } catch (Exception e) { e.printStackTrace(); } finally { rd.close(); inputStream.close(); buffer.close(); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Andriy
Level 22 , Toronto, Canada
3 September 2019, 14:53
Yes, it helped, thanks a lot :)
0
Vahan
Level 41 , Tbilisi, Georgia
3 September 2019, 07:24solution
In accordance with conditions you must read path from console, so, as i believe, in parameter of FileInputStream (line 30) must be only fileName, which value is path to the some file.txt. So you needn't buffer object (line 30) and related expressions. Accordingly, in loop must be used inputStream, not buffer. .
+2
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.