-3

When I run the code below, I get an error.
Can anyone please help me solve the problem?

Code:

object Tests {        
  new java.io.File(".").getAbsolutePath()               
  val source = io.Source.fromFile("C://Users//AlphaLy//Desktop//INSURANCE.docx")  
}

Error:

java.nio.charset.UnmappableCharacterException: Input length = 1 //| at java.nio.charset.CoderResult.throwException(Unknown Source) //| at sun.nio.cs.StreamDecoder.implRead(Unknown Source) //| at sun.nio.cs.StreamDecoder.read(Unknown Source) //| at java.io.InputStreamReader.read(Unknown Source) //| at java.io.BufferedReader.fill(Unknown Source) //| at java.io.BufferedReader.read(Unknown Source)

Tzach Zohar
  • 37,442
  • 3
  • 79
  • 85
Leealp
  • 3
  • 3
  • 1
    Welcome to Stack Overflow! Unfortunately, your post, as it is currently phrased, does not comply with the site's guidelines and is likely to be closed: it's missing a lot of information ("I get an error" - what is the error?) and some research (have you searched for how to read a file in Scala? There are many relevant answers on SO for that). – Tzach Zohar Jun 21 '17 at 18:30
  • Here is the error: //> java.nio.charset.UnmappableCharacterException: Input length = 1 //| at java.nio.charset.CoderResult.throwException(Unknown Source) //| at sun.nio.cs.StreamDecoder.implRead(Unknown Source) //| at sun.nio.cs.StreamDecoder.read(Unknown Source) //| at java.io.InputStreamReader.read(Unknown Source) //| at java.io.BufferedReader.fill(Unknown Source) //| at java.io.BufferedReader.read(Unknown Source) – Leealp Jun 21 '17 at 18:33
  • Please [edit](https://stackoverflow.com/posts/44683465/edit) the post to add necessary information (instead of adding it in comments where it's easy to miss and hard to format clearly). – Tzach Zohar Jun 21 '17 at 18:34
  • Also missing: what exactly are you trying to achieve? It seems that you're trying to read a _binary_ file (`.docx`) into a _String_, that's probably not useful even if you can get it to work; What do you expect `source` to contain? – Tzach Zohar Jun 21 '17 at 18:39
  • I'm trying to read a file I saved at the path indicated in the code. – Leealp Jun 21 '17 at 18:43
  • 1
    That did not answer my question; What are you trying to read the file _for_ - do you expect to use it as a String (if so - you *can't*, if it's a `docx` file it's not a textual file)? Are you trying to load it into a _byte array_? what should `source` contain? It's impossible to answer your question as your intention is not clear. – Tzach Zohar Jun 21 '17 at 18:51
  • I'm learning scala. I"m not an expert. I'm just trying to open the file and edit some lines. Just for learning purpose. – Leealp Jun 21 '17 at 19:29
  • My question is simple. Why am I getting an error first place. It doesn't matter what I'm trying to do with the file. new java.io.File(".").getAbsolutePath() didn't give me any errors. Why the second one returns error? That's my question. – Leealp Jun 21 '17 at 19:40

1 Answers1

0

If you want to read the file to edit lines, the file should contain lines to begin with;

A docx file is binary - it cannot be viewed as text and hence does not have "lines" at all; It cannot be read into a String in any sensible way - and that's what this exception means - UnmappableCharacterException because some characters (bytes) in the input cannot be mapped to any textual symbol.

Try saving a textual file format (e.g. using "Save as CSV" from Excel) and loading it; That should work, and if it doesn't - see Scala java.nio.charset.UnmappableCharacterException: Input length = 1 .

Tzach Zohar
  • 37,442
  • 3
  • 79
  • 85
  • Thank you very much Tzach. I'll try. But the doc is a word document and I simply clicked on properties and copied the link. I didn't know it was a binary file. I'll try saving it differently and hope it will work. Thanks – Leealp Jun 21 '17 at 19:50
  • Thank you very much Tzach. I saved it as a txt file and it's working. Thanks – Leealp Jun 21 '17 at 20:02
  • Glad to hear; To let other readers know this was helpful, you can upvote this answer and/or accept it – Tzach Zohar Jun 21 '17 at 20:03