Does Scala / java have something like StringIO from python?

I would like to know that if java / scala has a "string object that can act as a file" like StringIO in python? I believe this would be better than writing and reading a lot of temporary file. I prefer scala, but java should be ok too.

+7
source share
3 answers

It depends on how it is used. You see, while you are doing stuff with a file in Python, you're not doing anything with File in Java! Well, besides tasks like checking permission, creating, etc.

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

  • InputStream and OutputStream
  • Reader and Writer

What you do is create one of these classes by passing the File as parameter. So, if any of the APIs you use intend to get a File , you can't do anything about it. However, the API, as a rule, takes one of the above classes, not File , and they all have an available version of the string.

As for Scala, then also scala.io.Stream , for which you can also create it based on String .

+3
source

I believe StringWriter is what you are looking for.

+11
source

Java has a ByteArrayOutputStream .

 OutputStream out = new ByteArrayOutputStream(); out.write(...); String result = new String(out.toByteArray()); 
+1
source

All Articles