This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File reading and manipulation  RSS feed

 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a requirement with regards to i/o and hope someone can help me.
In my java swing application, I have a text file (to store user preferences) which I have included in the jar file itself. When my application is launched I read this text file from the jar file using java.util.jar classes. Partial code to do so is as below

Now I have all the information from the text file in the variable "iStr" as bytes. My question is how can I convert it to text ie how to have each line.
One way of achiving this is to dump all the info in "iStr" into a temporary file using FileOutputStream and then read the temporary file. But I am looking if I can do away with creating a temporary file. Ofcouse I can delete the temporary file at the end but still I think it will be nice to manipulate the data in memory itself.
Thanks
Ash
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ash
Try,
BufferedReader br = new BufferedReader(new InputStreamReader(iStr));
String line = null;
line = br.readLine();
while ( line != null ) {
// do whatever you want with this 'line' variable
line = br.readLine();
}
br.close();
Does this solve your problem?
Regards
Maulin
 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maulin,
It did worked.
Thanks
Ashish
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!