一、JS File轉Byte數組
在前端開發中,我們常常需要將文件轉換為Byte數組,比如將用戶上傳的圖片轉成Blob來進行上傳。我們可以通過FileReader來實現將JS File對象轉換為Byte數組。
function fileToBytes(file) { return new Promise((resolve, reject) => { const fileReader = new FileReader(); fileReader.onload = (event) => { resolve(new Uint8Array(event.target.result)); }; fileReader.onerror = (event) => { reject(event.target.error); }; fileReader.readAsArrayBuffer(file); }); }
上述示例代碼使用Promise來非同步處理轉換過程,可以通過調用該函數來將一個JS File對象轉換為Byte數組。
二、File轉Bytes
在Java開發中,我們可以使用InputStream將File對象轉成Byte數組。
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public static byte[] fileToBytes(String filePath) throws IOException { Path path = Paths.get(filePath); return Files.readAllBytes(path); }
上述示例代碼將一個文件的路徑作為參數傳入,利用Java的Files類來讀取文件的所有位元組,並將其轉成Byte數組返回。
三、String轉Byte數組
在後端開發中,我們經常需要將一個字元串轉換成Byte數組進行加密或者傳輸。以下是一個Java示例代碼。
String str = "Hello, World!"; byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
上述代碼使用getBytes方法將字元串轉換成Byte數組,其中參數為指定編碼格式。
四、File轉成Byte數組
在.NET開發中,我們可以使用FileStream將File對象轉換為Byte數組。
using System.IO; public static byte[] fileToBytes(string filePath) { using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); return memoryStream.ToArray(); } } }
上述示例代碼使用FileStream類來打開一個文件,並利用MemoryStream類來讀取並轉換成Byte數組。
五、Byte數組轉File對象
在Java開發中,我們可以使用FileOutputStream來將Byte數組寫入到一個新的文件對象中。
import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public static void bytesToFile(byte[] bytes, String filePath) throws IOException { Path path = Paths.get(filePath); Files.write(path, bytes); }
上述示例代碼使用Files類來將Byte數組寫入到一個新的文件對象中。
六、Byte數組轉String
在Java開發中,我們可以使用String的構造函數將Byte數組轉換為字元串。
byte[] bytes = new byte[]{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}; String str = new String(bytes, StandardCharsets.UTF_8);
上述示例代碼使用StandardCharsets類來指定轉換編碼格式,將一個Byte數組轉換成字元串。
七、Byte數組轉文件
在.NET開發中,我們可以使用FileStream將Byte數組轉換為文件對象。
using System.IO; public static void bytesToFile(byte[] bytes, string filePath) { using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { stream.Write(bytes, 0, bytes.Length); } }
上述示例代碼使用FileStream類來創建一個新的文件,並將Byte數組寫入其中。
八、Byte數組轉Byte
我們可以通過位運算符來將兩個Byte數組合併成一個Byte。
public static byte bytesToByte(byte[] bytes1, byte[] bytes2) { int n1 = bytes1.length; int n2 = bytes2.length; byte[] bytes = new byte[n1 + n2]; System.arraycopy(bytes1, 0, bytes, 0, n1); System.arraycopy(bytes2, 0, bytes, n1, n2); byte b = 0; for (byte by : bytes) { b ^= by; } return b; } byte[] bytes1 = new byte[]{1, 2, 3}; byte[] bytes2 = new byte[]{4, 5, 6}; byte b = bytesToByte(bytes1, bytes2);
上述示例代碼使用System類中的arraycopy方法將兩個Byte數組合併成一個新的Byte數組,並通過異或運算來得到合併後的Byte。
九、Byte數組轉JSON
在Java開發中,我們可以使用Gson來將Byte數組轉換為JSON格式。
import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import java.nio.charset.StandardCharsets; public static String bytesToJson(byte[] bytes) { String jsonString = new String(bytes, StandardCharsets.UTF_8); Gson gson = new Gson(); JsonElement jsonElement = JsonParser.parseString(jsonString); return gson.toJson(jsonElement); }
上述示例代碼使用Gson和JsonParser來將Byte數組轉換為JSON格式。
總結
本文從多個方面詳細闡述了File轉Byte數組的實現方法。可以根據實際情況選擇不同的編程語言和方法進行操作。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286381.html