damian
damian

Reputation: 1419

Scala file reading adding spaces

I'm reading a file in scala using

def fileToString(that:String):String= {
    var x:String="" 
    for(line <- Source.fromFile(that).getLines){ 
        x += line + "\n"
    }
  x
  }

This works fine for a scala file. But on a txt file it adds spaces between every character. For example. I read in a .txt file and get this:

C a l l E v e n t L o g ( E r r o r $ , E r r N u m , E r r O b j )

' E n d E r r o r h a n d l i n g b l o c k .

E n d S u b

and I read in the scala file for the program and it comes out normally

EDIT: It seems to be something to do with Encoding. When I change it to UTF-16, it reads the .txt file, but not the scala file. Is there a way to make it universally work?

Upvotes: 0

Views: 662

Answers (3)

Donald.McLean
Donald.McLean

Reputation: 899

It's hard to be sure, but it sounds like the two files were written with different encodings. On any Unix system (including Mac) you can use the command od to look at the actual bytes in the file.

UTF-8 is the standard for ordinary text files on most systems, but if you have a mix of UTF-8 and UTF-16, you'll have to know which encoding to use for which files and correctly specify the encoding.

Or be more careful when you create the files to insure that they are all in the same format.

Upvotes: 0

Malte Schwerhoff
Malte Schwerhoff

Reputation: 12852

In general, if you read from a file you need to know its encoding in order to correctly read the characters. I am not sure what the default encoding is that Scala assumes, probably UTF8, but you can either pass a Codec to fromFile, or specify the encoding as a string:

io.Source.fromFile("file.txt", "utf-8")

Upvotes: 1

Kaito
Kaito

Reputation: 1765

No it can't work for all files. To read/interpret a file/data you need to know the format/encoding unless you're treating it as a binary blob.

Either save all files in the usual unicode format (UTF-8) or specify the encoding when reading the file.
FromFile takes an implicit codec, you can pass it explicitly.

io.Source.fromFile("123.txt")(io.Codec("UTF-16"))

Upvotes: 5

Related Questions