添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class JacksonObjectMapper {
    public static void main(String[] args) throws IOException {
        byte[] jsonData = Files.readAllBytes(Paths.get("employee.txt"));
        ObjectMapper objectMapper = new ObjectMapper();
        Employee emp = objectMapper.readValue(jsonData, Employee.class);
        System.out.println("Employee Object\n"+emp);
        Employee emp1 = createEmployee();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        StringWriter stringEmp = new StringWriter();
        objectMapper.writeValue(stringEmp, emp1);
        System.out.println("Employee JSON is\n"+stringEmp);

Now I have to run the the same code on Java 6 , what are the best possible alternatives other than using FileReader ?

org.apache.commons.io can help you check out this [link] commons.apache.org/proper/commons-io/apidocs/org/apache/commons/… – Darshan Patel Jan 10, 2014 at 8:47

In Files class source you can see that in readAllBytes method bytes are read from InputStream.

public static byte[] readAllBytes(Path path) throws IOException {
        long size = size(path);
        if (size > (long)Integer.MAX_VALUE)
            throw new OutOfMemoryError("Required array size too large");
        try (InputStream in = newInputStream(path)) {
             return read(in, (int)size);

return read(in, (int)size) - here it uses buffer to read data from InputStream.

So you can do it in the same way or just use Guava or Apache Commons IO http://commons.apache.org/io/.

Alternative are classes from java.io or Apache Commons IO, also Guava IO can help.

Guava is most modern, so I think it is the best solution for you.

Read more: Guava's I/O package utilities, explained.

If you really don't want to use FileReader(Though I didn't understand why) you can go for FileInputStream.

Syntax:

InputStream inputStream = new FileInputStream(Path of your file);
Reader reader = new InputStreamReader(inputStream);
                That has exactly the same effect as a FileReader and still has the problem of using the default encoding for the current platform rather than a fixed encoding that you know matches that of the file.
– Ian Roberts
                Jan 13, 2014 at 10:52

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);

You can read all bytes of a file into byte array even in Java 6 as described in an answer to a related question:

import java.io.RandomAccessFile;
import java.io.IOException;
RandomAccessFile f = new RandomAccessFile(fileName, "r");
if (f.length() > Integer.MAX_VALUE)
    throw new IOException("File is too large");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
if (f.getFilePointer() != f.length())
    throw new IOException("File length changed while reading");

I added the checks leading to exceptions and the change from read to readFully, which was proposed in comments under the original answer.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.