Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does scala/java has something like StringIO from python?

Tags:

java

scala

I would like to know that if java/scala has the "string object that could act as file" as StringIO in python ? I figure that it would be better than writing and reading alot of temporary file. I prefer scala but java one should be fine too.

like image 463
Tg. Avatar asked Nov 25 '11 00:11

Tg.


2 Answers

I believe StringWriter is what you're looking for.

like image 172
Michael Borgwardt Avatar answered Oct 19 '22 11:10

Michael Borgwardt


It depends on how this is being used. You see, while you do stuff with a file in Python, you don't do anything with a File in Java! Well, aside from tasks like checking permission, creating, etc.

All I/O in Java and Scala is based on one of two concepts:

  • InputStream and OutputStream
  • Reader and Writer

What you do is create one of these classes passing a File as parameter. So, if whatever API you are using is intent on receiving a File, you can't do anything about it. However, APIs will usually take one of the above classes, not a File, and all of them have a string-version available.

As for Scala, there's also scala.io.Stream, for which you can also create one based on a String.

like image 25
Daniel C. Sobral Avatar answered Oct 19 '22 11:10

Daniel C. Sobral