文件和byte数组之间相互转换

文件转换成byte数组

文件转换成byte数组有两种方式:

1. 传统方式

File file = new File("/temp/abc.txt");
//init array with file length
byte[] bytesArray = new byte[(int) file.length()];

FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();

return bytesArray;

2. NIO方式

String filePath = "/temp/abc.txt";

byte[] bFile = Files.readAllBytes(new File(filePath).toPath());
//or this
byte[] bFile = Files.readAllBytes(Paths.get(filePath));

完整实例

下面的例子展示了如何把读取的文件内容转换成byte数组,使用了传统的FileInputStreamjava.nio两种方式。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author zkzong
 * @date 2017/10/20
 */
public class FileToArrayOfBytes {
    public static void main(String[] args) {

        try {

            // convert file to byte[]
            byte[] bFile = readBytesFromFile("test.txt");

            //java nio
            //byte[] bFile = Files.readAllBytes(new File("test.txt").toPath());
            //byte[] bFile = Files.readAllBytes(Paths.get("test.txt"));

            // save byte[] into a file
            Path path = Paths.get("test2.txt");
            Files.write(path, bFile);

            System.out.println("Done");

            //Print bytes[]
            for (int i = 0; i < bFile.length; i++) {
                System.out.print((char) bFile[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static byte[] readBytesFromFile(String filePath) {

        FileInputStream fileInputStream = null;
        byte[] bytesArray = null;

        try {

            File file = new File(filePath);
            bytesArray = new byte[(int) file.length()];

            //read file into bytes[]
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bytesArray);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        return bytesArray;

    }
}

byte数组转换成文件

byte数组转换成文件也有两种方式:

1. 传统方式

FileOutputStream fos = new FileOutputStream(fileDest);
fos.write(bytesArray);
fos.close();

2. NIO方式

Path path = Paths.get(fileDest);
Files.write(path, bytesArray);

完整实例

下面的例子展示了如何把读取的文件内容转换成byte数组,并把保存的byte数组转换成一个新的文件,使用了传统的try-catch、JDK 7的try-resourcesjava.nio两种方式。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author zkzong
 * @date 2017/10/20
 */
public class ArrayOfBytesToFile {
    private static final String UPLOAD_FOLDER = "/";

    public static void main(String[] args) {

        FileInputStream fileInputStream = null;

        try {

            File file = new File("test.txt");
            byte[] bFile = new byte[(int) file.length()];

            //read file into bytes[]
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bFile);

            //save bytes[] into a file
            writeBytesToFile(bFile, UPLOAD_FOLDER + "test1.txt");
            writeBytesToFileClassic(bFile, UPLOAD_FOLDER + "test2.txt");
            writeBytesToFileNio(bFile, UPLOAD_FOLDER + "test3.txt");

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //Classic, < JDK7
    private static void writeBytesToFileClassic(byte[] bFile, String fileDest) {

        FileOutputStream fileOuputStream = null;

        try {
            fileOuputStream = new FileOutputStream(fileDest);
            fileOuputStream.write(bFile);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOuputStream != null) {
                try {
                    fileOuputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //Since JDK 7 - try resources
    private static void writeBytesToFile(byte[] bFile, String fileDest) {

        try (FileOutputStream fileOuputStream = new FileOutputStream(fileDest)) {
            fileOuputStream.write(bFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //Since JDK 7, NIO
    private static void writeBytesToFileNio(byte[] bFile, String fileDest) {

        try {
            Path path = Paths.get(fileDest);
            Files.write(path, bFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 106,544评论 12 127
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 26,970评论 17 394
  • 百战程序员_ Java1573题 QQ群:561832648489034603 掌握80%年薪20万掌握50%年薪...
    Albert陈凯阅读 13,438评论 3 33
  • 今年一直有个心愿去写写久远的过去,但脑海里似乎总有个声音说我做不到,因为的确太久太久了,生怕自己什么也想不起来,从...
    王五月阅读 113评论 0 1
  • 经过一次有一次的摧残和灵魂教育后,我们终于不再是整天冒泡的小年轻了, 十月的鄂北地区还依旧是那么燥热,晴朗的天空没...
    沧桑泯世阅读 109评论 0 5