
文件转换成 byte 数组
文件转换成 byte 数组有两种方式:
1. 传统方式
File file = new File("/temp/abc.txt");
FileInputStream fis = new FileInputStream(file);
byte[] bytesArray = new byte[(int)file.length()]; //init array with file length
fis.read(bytesArray); //read file into bytes[]
fis.close();
return bytesArray;或者
File file = new File("D:/a.jpg");
FileInputStream is=new FileInputStream(file);
byte[] bytes=new byte[is.available()];
is.read(bytes);
is.close();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 数组转换成文件
byte 数组转换成文件也有两种方式:
1. 传统方式
FileOutputStream fos = new FileOutputStream(fileDest); fos.write(bytesArray); fos.close();
2. NIO 方式
Path path = Paths.get(fileDest); Files.write(path, bytesArray);
作者:Java_Explorer
链接:https://www.jianshu.com/p/b8b8f1ded401
目录
文件转换成 byte 数组
- 1. 传统方式
- 或者
- 2. NIO 方式
byte 数组转换成文件
- 1. 传统方式
- 2. NIO 方式
目录
文件转换成 byte 数组
- 1. 传统方式
- 或者
- 2. NIO 方式
byte 数组转换成文件
- 1. 传统方式
- 2. NIO 方式