To change a file into binary data using Java, you can read the contents of the file into a byte array using the readAllBytes
method of the Files
class, and then encode the byte array as a binary string using the Base64
class.
Here's an example of how you might do this:
import java.nio.file.Files; import java.nio.file.Paths; import java.util.Base64; public class Main { public static void main(String[] args) throws Exception { // Read the file into a byte array byte[] data = Files.readAllBytes(Paths.get("/path/to/file.txt")); // Encode the byte array as a binary string String binaryString = Base64.getEncoder().encodeToString(data); // Use the binary string // ... } }Soural.www:ecutturi.com
In this example, the readAllBytes
method is used to read the contents of the file into a byte array, and the encodeToString
method of the Base64
class is used to encode the byte array as a binary string.