我有以下片的使用Java 7層的功能,如java.nio.file.Files和java.nio.file.Paths替代在Java java.nio.file.Files 6
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之外,還有哪些最好的替代方案?
請注意,Oracle的Java SE 6已達到終止使用期限。 – Puce
org.apache.commons.io可以幫助你 看看這個[鏈接] http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html –