Java: Write Byte Array to File
You can use the FileOutputStream class to write a byte array to a file.
Here is an example code:
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteByteArrayToFile {
public static void main(String[] args) {
try {
byte[] byteArray = {65, 66, 67, 68, 69}; // 生成一个byte数组
FileOutputStream fos = new FileOutputStream("output.txt");
fos.write(byteArray); // 将byte数组写入文件
fos.close();
System.out.println("Byte array has been written to file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, we begin by creating a byte array, then use the FileOutputStream class to create an output stream and write the byte array to the specified file. Finally, we close the output stream.
After running this code, a file named output.txt will be generated in the current directory, and the contents of the file will be the same as the byte array.