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

I have the following code snippet that uses java 7 functions 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 need to run the same Java 6 code, what are the best alternatives besides using FileReader?

+4
source share
5 answers

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

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

Details: Guava I / O package utilities explained.

+2
source

FileReader ( ), FileInputStream.

:

InputStream inputStream = new FileInputStream(Path of your file);
Reader reader = new InputStreamReader(inputStream);
+2

Files , readAllBytes 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) - InputStream.

, Guava Apache Commons IO http://commons.apache.org/io/.

+2

, FileReader, , , , JSON .

ObjectMapper has an overload readValuethat can read directly from File, there is no need to buffer the contents temporarily byte[]:

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

You can read all bytes of a file into a byte array even in Java 6, as described in the answer to the corresponding 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 checks leading to exceptions, and a change from readto readFully, which was suggested in the comments on the original answer.

+1
source

All Articles