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();
}
}
}
"The program displays too few lines." What is wrong?
Archived
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Andriy
3 September 2019, 14:53
Yes, it helped, thanks a lot :)
0
Vahan
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