-1

My question may be something really basic, but I am not able to find a working solution.

In my code I have three new InputStreams.

InputStream fstream = WasuTimeTool.class.getResourceAsStream("/resources/dbConParam.txt");
BufferedReader dbParamReader = new BufferedReader(new InputStreamReader(fstream));
String dbParamLine;

Simple as that. Everyone is the same beside the InputStreamString and the variables. They are build like that:

Stream One {

}

Stream Two {
    Stream Three {
    }
}

When I build a global Streams I got some problems with the use of the variable and the collision from the second and third stream. Is there a solutions to get one single Stream and make new Instances or should they stay as three different streams?

wg15music
  • 199
  • 4
  • 13
  • 2
    Your question is really unclear. Are you saying that your `dbConParam.txt` file somehow contains three distinct streams? I doubt that very much. – Kayaman Aug 18 '15 at 10:41
  • no, i'll edit and clear it up a bit, I have three different Streams, and everyone opens a new other .txt. But to do that I have three times the three lines at the top with different variables, what I want to do is a "global" stream and being able to make instances of that – wg15music Aug 18 '15 at 10:45
  • That's a very bad idea. You don't want to have any "global" variables. Why not make a method that opens a stream based on given parameters instead. – Kayaman Aug 18 '15 at 10:48
  • I tried that, but It did not work, or better said, the method to open the stream works, but everything after that does not. Would not save code lines at all – wg15music Aug 18 '15 at 10:50
  • Then you tried wrong, or are you claiming I'm a liar? It'll avoid the code duplication and make you forget dreaming about global variables. – Kayaman Aug 18 '15 at 10:51

1 Answers1

1
InputStream fstream = WasuTimeTool.class.getResourceAsStream("/resources/dbConParam.txt");
BufferedReader dbParamReader = new BufferedReader(new InputStreamReader(fstream));
String dbParamLine;

Is more like

Stream one{
   Stream two{
       Stream three{
       }
   }
}

This is correct and ok.

Remember: Closing of the BufferedReader (aka Stream one) will close the InputStreamReader and the InputStream too!

Sometimes the Streams internally have a small cache. To respect that behaviour you shall use the Stream with the most abstract level, BufferedReader in your example so faar.

Example

If you have this file:

Hello
Friend

And you read Hello\nFri using the InputStream, then if you use readLine from BufferedReader you will become end. This is bad because it is not the whole line we accept from the method readLine!

Solution

You should do this:

BufferedReader dbParamReader = new BufferedReader(new InputStreamReader(WasuTimeTool.class.getResourceAsStream("/resources/dbConParam.txt")));
String dbParamLine;

So noone can be inveigled to use the InputStream directly.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • So I was doing right that I should not change or, or better said, I can not really change it? That's the problem I met so far, the closing streams for example. – wg15music Aug 18 '15 at 10:48