将文件内容转换为字节数组以及反向转换的 Java 程序
FileInputStream 类包含方法 read(),此方法接受一个字节数组作为参数,并将文件输入流的数据读到给定的字节数组中。假设 myData 文件包含如下数据−

示例
import java.io.File;
import java.io.FileInputStream;
public class FileToByteArray {
public static void main(String args[]) throws Exception{
File file = new File("myData");
FileInputStream fis = new FileInputStream(file);
byte[] bytesArray = new byte[(int)file.length()];
fis.read(bytesArray);
String s = new String(bytesArray);
System.out.println(s);
}
}输出
Hi how are you welcome to Tutorialspoint
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP