使用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