problems with java.io and scala
I am a beginner in scala, and i tried to write this little programm:
import scala.io.Source
import java.io._
class Webpage(var adresse:String){
def copy(datei:String){
var text=new FileWriter(datei)
var page=Source.fromURL(adresse)
while(page.hasNext) text.write(page.next)
text.close
}
}
when i run this code in the console (scalac page.scala => scala page) it works quite well:
object page{
def main(args: Array[String]):Unit={
var page=new Webpage("http://www.schneidertraum.de/webshop/")
page.copy("C:/Info/Page.html")
}
}
But if I try to run it in IntelliJ i get the error
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.read(BufferedReader.java:157)
at scala.io.BufferedSource$$anonfun$1$$anonfun$apply$mcI$sp$1.apply$mcI$sp(BufferedSource.scala:28)
at scala.io.Codec.wrap(Codec.scala:63)
at scala.io.BufferedSource$$anonfun$1.apply(BufferedSource.scala:28)
at scala.io.BufferedSource$$anonfun$1.apply(BufferedSource.scala:28)
at scala.collection.Iterator$$anon$13.next(Iterator.scala:149)
at scala.collection.Iterator$$anon$24.hasNext(Iterator.scala:484)
at scala.collection.Iterator$$anon$19.hasNext(Iterator.scala:368)
at scala.io.Source.hasNext(Source.scala:239)
at Zettel_13.Webpage.copy(Webpage.scala:16)
at Zettel_13.page$.main(page.scala:14)
at Zettel_13.page.main(page.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
I readed that it has something to do with UTF8 and the java.io, but i have only a little bit understanding of this topic, and I would be thankfull if you could help me.
PS. With an other side it works in IntelliJ also:
def main(args: Array[String]):Unit={
var page=new Webpage("http://de.wikipedia.org/wiki/Last.fm")
page.copy("C:/Info/Page.html")
}
}So it has something to do with the content of the sidecode
niratschi
Please sign in to leave a comment.
IDEA instantiates JVM with "Settings / File Encodings / IDE encoding" encoding (check -Dfile.encoding=... parameter in the first line of run string).
It seems that you choose UTF8 as IDE encoding, while http://www.schneidertraum.de/webshop/ uses iso-8859-1.
Either add -Dfile.encoding=iso-8859-1 to VM parameters of Run configuration, or (better) change Source.fromURL(adresse)" to Source.fromURL(adresse, "iso-8859-1").
P.S. Web servers provide content encoding in HTTP headers, try to use Apache HttpClient library to handle them automatically.
thanks for this fast answer, it works now.