4

I have the following code

Properties influxProps = new Properties();
InputStream inputStream = this.getClass().getResourceAsStream("/influx.properties");
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
System.out.println("INFLUX PROPERTIES : "+result);
influxProps.load(inputStream);
System.out.println("Properties: "+influxProps.toString());

I am able to fetch the file and the result also displays the contents of the file correctly.

But loading of the input stream into influx properties doesn't work. When I try to print out the properties map it prints out nothing.

How do I resolve this. I am not running this code inside a static method.

shmosel
  • 49,289
  • 6
  • 73
  • 138
Niranjan
  • 517
  • 2
  • 4
  • 21
  • 8
    Streams are stateful. Once you convert it to string, you're at the end of the stream, so there's nothing left to read. You'll need to open the file again. – shmosel May 18 '18 at 02:45
  • Yes, fixed that works now. – Niranjan May 18 '18 at 02:48
  • 1
    Since you already loaded the file into memory, use it: `influxProps.load(new StringReader(result))` --- Also, remember to close the `inputStream`. – Andreas May 18 '18 at 03:02
  • Possible duplicate of [Read stream twice](https://stackoverflow.com/questions/9501237/read-stream-twice) – Bentaye May 18 '18 at 10:58

0 Answers0