0

My program can write an utf-8 file without a 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 I want to read it back 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 does not exist at " + f.toString());

return lesson;
}

I always get this error

org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0x22 at [Source: C:\Users\Username\Documents\VocabelTrainer\Filename; line: 1, column: 31] at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433) at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521) at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2830) at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2837) at org.codehaus.jackson.impl.Utf8StreamParser._decodeUtf8_2(Utf8StreamParser.java:2625) at org.codehaus.jackson.impl.Utf8StreamParser._finishString2(Utf8StreamParser.java:1952) at org.codehaus.jackson.impl.Utf8StreamParser._finishString(Utf8StreamParser.java:1905) at org.codehaus.jackson.impl.Utf8StreamParser.getText(Utf8StreamParser.java:276) at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:26) at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:13) at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299) at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697) at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217) at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194) at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30) at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299) at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697) at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1817) at vocabeltrainer.file.ReadFile.main(ReadFile.java:26) at vocabeltrainer.main.FileNameSwing.getFileNames(FileNameSwing.java:232) at vocabeltrainer.main.FileNameSwing.(FileNameSwing.java:44) at vocabeltrainer.main.Swing.actionPerformed(Swing.java:87) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

because he reads in the letter 'ß' which is an UTF-8 Letter!

I read severeal articles but I didn't find a proper way to read UTF-8 in.

Thanks

GC268DM
  • 403
  • 7
  • 15
  • See https://stackoverflow.com/questions/6352861/jackson-jsonparseexception-invalid-utf-8-middle-byte – Raedwald Aug 23 '18 at 08:20

2 Answers2

0

You don't specify the encoding when you write to the file; it is probable that the JRE's default file encoding is not UTF-8, hence the error.

Instead of using:

new FileWriter(file)

try and use (JDK7):

Files.newBufferedWriter(Paths.get("/path/to/file"), StandardCharsets.UTF_8)

If JDK6:

new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8"))

Similarly, for reading, use Files.newBufferedReader().

fge
  • 119,121
  • 33
  • 254
  • 329
0

I don't know why the first and second encoding doesn't work the same way, byt you should always specify your encoding (UTF-8) when creating Writer/reader (all things that takes bytes and gives char an also do the opposite)

Write a file in UTF-8 using FileWriter (Java)?

So it is

new OutputStreamWriter(new FileOutputStream(file), "UTF-8")

and

new InputStreamReader(new FileInputStream(file), "UTF-8")

instead of FileReader, FileWriter

Community
  • 1
  • 1
pdem
  • 3,880
  • 1
  • 24
  • 38
  • First of all thanks, no exception is thrown now! But instead of "ßßßß" it returns "�����" – GC268DM Mar 10 '14 at 14:39