Alternative to java.nio.file.Files in Java 6

前端 未结 5 1851
北荒
北荒 2021-01-18 17:33

I have the following piece of code that uses the java 7 features like java.nio.file.Files and java.nio.file.Paths

import java.io.File;
impor         


        
5条回答
  •  独厮守ぢ
    2021-01-18 18:17

    You are right to avoid FileReader as that always uses the default character encoding for the platform it is running on, which may not be the same as the encoding of the JSON file.

    ObjectMapper has an overload of readValue that can read directly from a File, there's no need to buffer the content in a temporary byte[]:

    Employee emp = objectMapper.readValue(new File("employee.txt"), Employee.class);
    

提交回复
热议问题