Java 6中java.nio.file.Files的替代方法 [英] Alternative to java.nio.file.Files in Java 6

查看:378
本文介绍了Java 6中java.nio.file.Files的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码使用java 7的功能,如 java.nio.file.Files和java.nio.file.Paths

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

现在我必须运行相同的Java 6上的代码,除了使用FileReader之外,还有哪些最佳选择?

推荐答案

替代方案是来自< a href =http://docs.oracle.com/javase/6/docs/api/java/io/package-summary.html\"rel =nofollow> java.io 或 Apache Commons IO ,也是 Guava 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.

了解更多: Guava的I / O包实用程序,解释。

这篇关于Java 6中java.nio.file.Files的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆