使用Java将 byte[] 数组转换为 File
正如我们所知道的,每当涉及到写入文件时,File 类的 write() 方法就会发挥作用,但在这里我们不能使用它来将该字节转换为文件。为了将字节数组转换为文件,我们将使用 String 类的getBytes()方法。
实现:将字符串转换为字节数组并将其写入文件中。
例子:
Java
// 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);
}
}
Java
// 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 = "";
// Gettig 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 exeptions 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
现在让我们也讨论一下用例,以便如何在文件中写入整数、双精度、字符值(使用 Wrapper 类)
例子:
Java
// 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 = "";
// Gettig 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 exeptions 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