import java.io.*;
import java.util.*;
/*
Introducing properties
*/
public class Solution {
public static Map<String, String> properties = new HashMap<>();
public void fillInPropertiesMap() throws Exception {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader.readLine();
bufferedReader.close();
try(FileInputStream inputStream = new FileInputStream(fileName)){
load(inputStream);
}
}
public void save(OutputStream outputStream) throws Exception {
Properties properties1 = new Properties();
for(Map.Entry<String,String> entry: properties.entrySet()){
properties1.setProperty(entry.getKey(), entry.getValue());
}
properties1.store(outputStream,"");
}
public void load(InputStream inputStream) throws Exception {
Properties properties1 = new Properties();
properties1.load(inputStream);
Set<String> propertyNames = properties1.stringPropertyNames();
for(String s: propertyNames){
properties.put(s,properties1.getProperty(s));
}
}
public static void main(String[] args) throws Exception {
Solution solution = new Solution();
solution.fillInPropertiesMap();
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader1.readLine();
try(FileOutputStream outputStream = new FileOutputStream(fileName)){
solution.save(outputStream);
}
}
}
I've passed the task with the above code minus the code in the main method. That was just for me to try it out on my machine. But why do I get the following error after I pass in the first filename and after solution.fillInPropertiesMap(); finished it's work. Instead of getting the possibility to put in a second file name I get this:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:170)
at java.io.BufferedInputStream.read(BufferedInputStream.java:336)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.codegym.task.task20.task2003.Solution.main(Solution.java:44)
where line 44 is String fileName = bufferedReader1.readLine(); in my code. But as soon as I delete bufferedReader.close(); in line 15 everything works fine. But I opened a new stream. who is my "stream closed" apparently.Willi Ehrmann
Level 32
Why do I get this strange error?
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
12 April, 06:59useful
In line 15 when you close the BufferedReader, you also close all nested streams, the InputStreamReader and System.in.
In line 41 you can create a new Reader again but the underlying PrintStream (System.in) is already closed and you get your exception when trying to access it. So usually you use a helper class to handle input. Here the BufferedReader is a (static) property of the class so you do not need to close it.
Inline (inside the Solution class as field) that'll look like:
+1