使用Java将byte[]数组转换为文件
在Java文件类中,文件类表示不同平台上不同格式的目录路径名。Java中的文件类包含不同路径名的处理方法,负责使用新的目录 删除和重命名文件。它是一个字符串形式的抽象类,可以是绝对路径或相对路径。
将Byte[ ]数组转换为文件的算法
在这个可能的算法中,我们将向您展示如何对byte()数组节点进行转换过程,使其成为一个文件。通过使用此算法,我们将构建一些Java语法来更广泛地了解问题陈述。
步骤1 - 开始进程。
步骤2 - 声明并导入Java包。
步骤3 - 声明一个公共类。
步骤4 - 声明一个将字节数组转换为Writer类的方法。
步骤5 - 声明一个静态字节类型。
步骤6 - 声明一个文件写入器类。
步骤7 - 声明一个调用append()方法。
步骤8 - 构造驱动代码。
步骤9 - 声明各种值。
步骤10 - 调用主方法。
步骤11 - 获取返回值作为文件。
步骤12 - 终止进程。
将Byte[ ]数组转换为文件的语法
在这个可能的算法中,我们将向您展示如何对byte()数组节点进行转换过程,使其成为一个文件。通过使用此算法,我们将构建一些Java语法来更广泛地了解问题陈述。
步骤1 - 开始进程。
步骤2 - 声明并导入Java包。
步骤3 - 声明一个公共类。
步骤4 - 声明一个将字节数组转换为Writer类的方法。
步骤5 - 声明一个静态字节类型。
步骤6 - 声明一个文件写入器类。
步骤7 - 声明一个调用append()方法。
步骤8 - 构造驱动代码。
步骤9 - 声明各种值。
步骤10 - 调用主方法。
步骤11 - 获取返回值作为文件。
步骤12 - 终止进程。
将Byte[ ]数组转换为文件的语法
FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)
try (FileOutputStream fos = new FileOutputStream("pathname")) {
fos.write(myByteArray);
}
try (FileOutputStream fos = new FileOutputStream("fullPathToFile")) {
fos.write(byteArray);
} catch (IOException ioe) {
ioe.printStackTrace();
}
String fileName = "file.test";
BufferedOutputStream bs = null;
try {
Convert byte[] array to File using Java
FileOutputStream fs = new FileOutputStream(new File(fileName));
bs = new BufferedOutputStream(fs);
bs.write(byte_array);
bs.close();
bs = null;
} catch (Exception e) {
e.printStackTrace()
}
if (bs != null) try { bs.close(); } catch (Exception e) {}
File f = new File(fileName);
byte[] fileContent = msg.getByteSequenceContent();
Path path = Paths.get(f.getAbsolutePath());
try {
Files.write(path, fileContent);
} catch (IOException ex) {
Logger.getLogger(Agent2.class.getName()).log(Level.SEVERE, null, ex);
}
Path path = Paths.get(p);
byte[] data = null;
try {
data = Files.readAllBytes(path);
} catch (IOException ex) {
Logger.getLogger(Agent1.class.getName()).log(Level.SEVERE, null, ex);
}
File f = new File(fileName);
byte[] fileContent = msg.getByteSequenceContent();
Path path = Paths.get(f.getAbsolutePath());
try {
Files.write(path, fileContent);
} catch (IOException ex) {
Logger.getLogger(Agent2.class.getName()).log(Level.SEVERE, null, ex);
}
在上述可能的语法中,我们尝试向您展示如何对byte()节点执行转换过程以使其成为文件。通过这些语法,我们将构建一些Java代码,以有效的方式解决问题陈述。
遵循的方法
方法1 - 使用java.io.FileOutputStream的Java程序将字节数组转换为文件,以定义与进程相关的异常以及writeByte(byteInt, byteChar, byteDouble)和writer的使用以及调用append()方法
方法2 - 使用java.io.ByteArrayInputStream和InputStream方法的子类的Java程序将字节数组转换为文件
方法1
使用Java.io.FileOutputStream定义与进程相关的异常并实现WriteByte(ByteInt, ByteChar, byteDouble)和Writer以及调用Append()方法
使用Java.io.FileOutputStream和异常方法
在这种可能的方法中,我们将对byte[]数组列表应用java.io.FileOutputStream方法。通过遵循此方法,我们将对该byte数组列表运行转换,通过定义异常将其转换为文件。
File outputFile = tempFolder.newFile("FILE NAME");
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
outputStream.write(dataForWriting);
}
public static void main(String[] args) throws IOException {
byte[] bytes = "frontbackend.com".getBytes(StandardCharsets.UTF_8);
try (FileOutputStream fos = new FileOutputStream("FILE PATH")) {
fos.write(bytes);
示例
//Java Program to convert Byte Array to File by using java.io.FileOutputStream to define exceptions related to the process
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class ARBRDD{
static String FILEPATH = "";
static File file = new File(FILEPATH);
static void writeByte(byte[] bytes){
try{
OutputStream os = new FileOutputStream(file);
os.write(bytes);
System.out.println("Successfully" + "the byte has been inserted");
os.close();
}
catch (Exception e){
System.out.println("Exception Is Here: " + e);
}
}
public static void main(String args[]){
String string = "TutorialsPoint" + " -Is A Computer Science" + "Data Source Portal For Coding Minds";
byte[] bytes = string.getBytes();
writeByte(bytes);
}
}
输出
Exception Is Here: java.io.FileNotFoundException: (No such file or directory)
使用具有WriteByte(ByteInt, ByteChar, ByteDouble)方法的多种方法
在这种可能的方法中,我们将对byte[]数组列表应用使用writeByte(byteInt, byteChar, byteDouble)方法的多方法。通过遵循此方法,我们将对该byte数组列表运行转换,通过定义异常将其转换为文件。
示例
// Java Program to Convert Integer, Character and Double types into Bytes and Writing it in a File by using multipale methods with writeByte(byteInt, byteChar, byteDouble)
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class ARBRDD{
static String FILEPATH = "";
static File file = new File(FILEPATH);
static void writeByte(byte[] byteInt, byte[] byteChar,
byte[] byteDouble){
try {
OutputStream os = new FileOutputStream(file);
os.write(byteInt);
os.write(byteChar);
os.write(byteDouble);
System.out.println(
"Successfully the byte has been inserted");
os.close();
}
catch (Exception e) {
System.out.println("Exception is here. Note it down: " + e);
}
}
public static void main(String args[]){
int num = 56;
char ch = 's';
double dec = 78.9;
byte[] byteInt = Integer.toString(num).getBytes();
byte[] byteChar = Character.toString(ch).getBytes();
byte[] byteDouble = Double.toString(dec).getBytes();
writeByte(byteInt, byteChar, byteDouble);
}
}
输出
Exception is here. Note it down: java.io.FileNotFoundException: (No such file or directory)
使用Writer和调用Append()方法的方法
在这种可能的方法中,我们将对byte[]数组列表应用使用writer和调用append()方法的多方法。通过遵循此方法,我们将对该byte数组列表运行转换,将其转换为文件。
示例
//Java Program Convert byte() array list to a file by using a writer and Call append() method
import java.io.StringWriter;
import java.io.Writer;
public class ARBRDD{
static String writeByte(byte[] byteString,
byte[] byteInt,
byte[] byteChar,
byte[] byteDouble){
Writer writer = new StringWriter();
try {
writer
.append(new String(byteString)
+ new String(byteDouble)
+ new String(byteChar)
+ new String(byteInt));
writer.close();
}
catch (Exception e) {
System.out.println("Exception Is Here: " + e);
}
return writer.toString();
}
public static void main(String args[]){
String str = "Speed of the light is: ";
int num = 7;
char ch = 'e';
double dec = 16.0;
// Insert String value
byte[] byteString = str.getBytes();
// Insert int value
byte[] byteInt = Integer.toString(num).getBytes();
// Insert char value
byte[] byteChar = Character.toString(ch).getBytes();
// Insert double value
byte[] byteDouble = Double.toString(dec).getBytes();
// Call the method
System.out.println(writeByte(byteString, byteInt,
byteChar,
byteDouble));
}
}
输出
Speed of the light is: 16.0e7
方法2
使用Java.io.ByteArrayInputStream和InputStream方法的子类。
使用Java.io.ByteArrayInputStream方法
在这种可能的方法中,我们将对byte[]数组列表应用使用java.io.ByteArrayInputStream方法的多方法。通过遵循此方法,我们将对该byte数组列表运行转换,将其转换为文件。
BufferedImage bImage = ImageIO.read(new File("FILE NAME"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "FILE TYPE", bos );
byte [] data = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(data);
BufferedImage bImage2 = ImageIO.read(bis);
ImageIO.write(bImage2, "FILE TYPE", new File("OUTPUT FILE") );
System.out.println("OUTPUT STATEMENT");
示例
//Java Program Convert byte() array list to a file by using a java.io.ByteArrayInputStream method
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ARBRDD{
public static void main(String a[]){
String str = "Converting to input stream byte[] array to file";
byte[] content = str.getBytes();
int size = content.length;
InputStream is = null;
byte[] b = new byte[size];
try {
is = new ByteArrayInputStream(content);
is.read(b);
System.out.println("Operation Completed: "+new String(b));
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(is != null) is.close();
} catch (Exception ex){
}
}
}
}
输出
Operation Completed: Converting to input stream byte[] array to file
使用InputStream方法的子类
在这种可能的方法中,我们将对byte[]数组列表应用使用InputStream方法子类的多方法。通过遵循此方法,我们将对该byte数组列表运行转换,将其转换为文件。
示例
//Java program to create InputStream from byte array ByteArrayInputStream is sub-class of InputStream
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class ARBRDD{
public static void main(String args[]) {
String str = "I AM AN INDIAN";
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
InputStream is = new ByteArrayInputStream(bytes);
String stringFromBytes = streamToString(is, StandardCharsets.UTF_8);
System.out.println("File recreated from bytes is: " + str);
OutputStream os = new ByteArrayOutputStream();
try {
os.write(bytes);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String streamToString(InputStream is, Charset encoding) {
BufferedReader br
= new BufferedReader(new InputStreamReader(is, encoding));
StringBuilder sb = new StringBuilder(1024);
try {
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
} catch (IOException io) {
System.out.println("Failed to read from the File");
io.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ioex) {
System.out.println("Failed to close the Files");
ioex.printStackTrace();
}
}
return sb.toString();
}
}
输出
File recreated from bytes is: I AM AN INDIAN
结论
在Java环境中,writer类用于写入字符流以传递与字节数组相关的参数。在本文中,我们学习了如何在Java环境中将特定的byte[]数组列表转换为特定文件的各种方法。通过使用上述语法和算法,我们构建了一些Java代码来有效地解释问题陈述。
另请阅读:Java面试问答
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP