Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

read a .txt file from classpath  RSS feed

 
Jeffrey Pony
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to read a .txt file from classpath containing values like this,

key = value
spaced key = spaced value

I have to capture the key and value.
I was intending to capture each line and use split or StringTokenizer to look for '=' to determine the key and value.

But on reading the file from classpath I get an InputStream. Is there any way to read each line in a file using InputStream?
Could someone provide a code snippet to the problem?

I could have used BufferedReader.readLine() if the file was in the working directory but the file is in the classpath and I have not found a way yet to read the file from the classpath and not obtain InputStream.
Can any one suggest how to obtain a Reader while reading a file from classpath?

Thanks in advance.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the Stream and Reader/Writer classes allow wrapping of various other types.InputStreamReader is the magic glue necessary to convert from a byte stream to a character reader. BufferedReader gives you buffering for performance and readLine().
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!