Scala - proper way to print a string to file -
what proper way print string - , string - file? when try standard way known me, i.e:
def printtofile(o:object,n:string) = try{ val pathtooutput = "..\\some\\parent\\directory\\" val path = paths.get(pathtooutput + n) val b = new bytearrayoutputstream() val os = new objectoutputstream(b) os.writeobject(o) files.write(path, b.tobytearray, standardopenoption.create, standardopenoption.truncate_existing) }catch{ case _:exception => println("failed write") }
it seems prepend
’ nul enqtstxt
where part after enqt
seems vary. (doesn't matter if declare o
an object or string.)
this annoying because want print couple of .dot-strings (graphviz) in order batch-process resulting .dot-files .pdf-files. prepended nonsense, however, forces me open each .dot-file , remove manually - kind of defeats purpose of batch-processing them.
this has nothing scala specifically, it's way java standard library works. when writeobject
writing serialized representation of object, bunch of additional bytes jvm can use re-create object. if know object string
, strong-type (i.e., use printtofile(o:string,n:string)
, can use files.write(path, o.getbytes, ...
. otherwise use o.tostring.getbytes
.
Comments
Post a Comment