Java 将byte[]数组转换为文件
我们知道,每当涉及到向文件写入时,文件类的write()方法就会发挥作用,但在这里我们不能用它来将字节转换成文件。为了将一个字节数组转换为文件,我们将使用String类的 getBytes() 方法。
实现 :将一个字符串转换为一个字节数组并写入文件。
例子
// Java Program to convert Byte Array to File
// Importing required classes
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
// Main class
public class GFG {
// Path of a file
static String FILEPATH = "";
static File file = new File(FILEPATH);
// Method 1
// To write the bytes into a file
static void writeByte(byte[] bytes)
{
// Try block to check for exceptions
try {
// Initialize a pointer in file
// using OutputStream
OutputStream os = new FileOutputStream(file);
// Starting writing the bytes in it
os.write(bytes);
// Display message onconsole for successful
// execution
System.out.println("Successfully"
+ " byte inserted");
// Close the file connections
os.close();
}
// Catch block to handle the exceptions
catch (Exception e) {
// Display exception on console
System.out.println("Exception: " + e);
}
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Input string
String string = "GeeksForGeeks"
+ " - A Computer Science"
+ " Portal for geeks";
// Getting byte array from string
// using getBytes() method
byte[] bytes = string.getBytes();
// Calling Method 1 to
// convert byte array to file
writeByte(bytes);
}
}
输出: 在控制台
Successfully byte inserted
现在让我们也来讨论一下如何在文件中写入整数、双数、字符值的用例(使用封装类)。
例子
// Java Program to Convert Integer, Character and Double
// Types into Bytes and Writing it in a File
// Importing required classes
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
// Main class
public class GFG {
// Path of a file
static String FILEPATH = "";
// Getting the file via creating File class object
static File file = new File(FILEPATH);
// Method 1
// Writing the bytes into a file
static void writeByte(byte[] byteInt, byte[] byteChar,
byte[] byteDouble)
{
// Try block to check for exceptions
try {
// Initialize a pointer in file
// using OutputStream class object
OutputStream os = new FileOutputStream(file);
// Starting writing the bytes in it
// Writing int value
os.write(byteInt);
// Writing char value
os.write(byteChar);
// Writing double value
os.write(byteDouble);
// Display message for successful execution of
// program
System.out.println(
"Successfully byte inserted");
// Close the file connections
// using close() method
os.close();
}
// Catch block to handle exceptions
catch (Exception e) {
// Display message when exceptions occurs
System.out.println("Exception: " + e);
}
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Declaring and initializing data types
int num = 56;
char ch = 's';
double dec = 78.9;
// Inserting integer value
byte[] byteInt = Integer.toString(num).getBytes();
// Inserting character value
byte[] byteChar = Character.toString(ch).getBytes();
// Inserting double value
byte[] byteDouble = Double.toString(dec).getBytes();
// Calling Method 1 to
// write the bytes into a file
writeByte(byteInt, byteChar, byteDouble);
}
}
输出: 在控制台
Successfully byte inserted