java - org.codehaus.jackson.JsonParseException when reading utf-8 file with object mapper (jackson all jar 1.9.0) -


my program can write utf-8 file without mistake

    public static boolean main(lesson lesson, int autosave) throws ioexception {         final objectmapper mapper = new objectmapper();          string user = system.getproperty("user.name");         string s = user.format("c:\\users\\%s\\documents", user);         s = s.replace("\\", "/");          file dir = new file(string.format(("%s/vocabeltrainer"), s));         dir.mkdir();          string path = string.format("%s/vocabeltrainer/%s", s, lesson.getlessonname());         file file = new file(path);         if(file.exists() == false || autosave == 1){             mapper.writevalue(new filewriter(path), lesson);             return true;         }         else             return false;  } 

but when want read in

public class readfile {  public static lesson main(string filename) throws jsonparseexception, jsonmappingexception, ioexception { final objectmapper mapper = new objectmapper(); lesson lesson = new lesson();  string user = system.getproperty("user.name"); string s = string.format("c:\\users\\%s\\documents\\vocabeltrainer", user); s = s.replace("\\", "/"); string path = string.format("%s/%s", s, filename); file f = new file(path);  if (f.exists())     lesson = mapper.readvalue(new file(path), lesson.class); else     system.out.println("file not exist @ " + f.tostring());  return lesson; } 

i error

org.codehaus.jackson.jsonparseexception: invalid utf-8 middle byte 0x22 @ [source: c:\users\username\documents\vocabeltrainer\filename; line: 1, column: 31] @ org.codehaus.jackson.jsonparser._constructerror(jsonparser.java:1433) @ org.codehaus.jackson.impl.jsonparserminimalbase._reporterror(jsonparserminimalbase.java:521) @ org.codehaus.jackson.impl.utf8streamparser._reportinvalidother(utf8streamparser.java:2830) @ org.codehaus.jackson.impl.utf8streamparser._reportinvalidother(utf8streamparser.java:2837) @ org.codehaus.jackson.impl.utf8streamparser._decodeutf8_2(utf8streamparser.java:2625) @ org.codehaus.jackson.impl.utf8streamparser._finishstring2(utf8streamparser.java:1952) @ org.codehaus.jackson.impl.utf8streamparser._finishstring(utf8streamparser.java:1905) @ org.codehaus.jackson.impl.utf8streamparser.gettext(utf8streamparser.java:276) @ org.codehaus.jackson.map.deser.std.stringdeserializer.deserialize(stringdeserializer.java:26) @ org.codehaus.jackson.map.deser.std.stringdeserializer.deserialize(stringdeserializer.java:13) @ org.codehaus.jackson.map.deser.settablebeanproperty.deserialize(settablebeanproperty.java:299) @ org.codehaus.jackson.map.deser.settablebeanproperty$methodproperty.deserializeandset(settablebeanproperty.java:414) @ org.codehaus.jackson.map.deser.beandeserializer.deserializefromobject(beandeserializer.java:697) @ org.codehaus.jackson.map.deser.beandeserializer.deserialize(beandeserializer.java:580) @ org.codehaus.jackson.map.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:217) @ org.codehaus.jackson.map.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:194) @ org.codehaus.jackson.map.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:30) @ org.codehaus.jackson.map.deser.settablebeanproperty.deserialize(settablebeanproperty.java:299) @ org.codehaus.jackson.map.deser.settablebeanproperty$methodproperty.deserializeandset(settablebeanproperty.java:414) @ org.codehaus.jackson.map.deser.beandeserializer.deserializefromobject(beandeserializer.java:697) @ org.codehaus.jackson.map.deser.beandeserializer.deserialize(beandeserializer.java:580) @ org.codehaus.jackson.map.objectmapper._readmapandclose(objectmapper.java:2732) @ org.codehaus.jackson.map.objectmapper.readvalue(objectmapper.java:1817) @ vocabeltrainer.file.readfile.main(readfile.java:26) @ vocabeltrainer.main.filenameswing.getfilenames(filenameswing.java:232) @ vocabeltrainer.main.filenameswing.(filenameswing.java:44) @ vocabeltrainer.main.swing.actionperformed(swing.java:87) @ javax.swing.abstractbutton.fireactionperformed(unknown source) @ javax.swing.abstractbutton$handler.actionperformed(unknown source) @ javax.swing.defaultbuttonmodel.fireactionperformed(unknown source) @ javax.swing.defaultbuttonmodel.setpressed(unknown source) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(unknown source) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)

because reads in letter 'ß' utf-8 letter!

i read severeal articles didn't find proper way read utf-8 in.

thanks

you don't specify encoding when write file; probable jre's default file encoding not utf-8, hence error.

instead of using:

new filewriter(file) 

try , use (jdk7):

files.newbufferedwriter(paths.get("/path/to/file"), standardcharsets.utf_8) 

if jdk6:

new outputstreamwriter(new fileoutputstream(file), charset.forname("utf-8")) 

similarly, reading, use files.newbufferedreader().


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -