Convert object to byte[] in Java

This article shows how to convert an object to byte[] or byte array and vice versa in Java. 1. Convert an object to byte[] The below example show how to use ByteArrayOutputStream and ObjectOutputStream to convert an object to byte[]. // Convert object to byte[] public static byte[] convertObjectToBytes(Object obj) { ByteArrayOutputStream boas = new …

Read more

Java – How to join and split byte arrays, byte[]

In this example, we will show you how to join and split byte arrays with ByteBuffer and System.arraycopy. ByteBuffer public static byte[] joinByteArray(byte[] byte1, byte[] byte2) { return ByteBuffer.allocate(byte1.length + byte2.length) .put(byte1) .put(byte2) .array(); } public static void splitByteArray(byte[] input) { ByteBuffer bb = ByteBuffer.wrap(input); byte[] cipher = new byte[8]; byte[] nonce = new byte[4]; …

Read more

Java – How to convert byte arrays to Hex

This article shows you a few ways to convert byte arrays or byte[] to a hexadecimal (base 16 or hex) string representative. String.format Integer.toHexString Apache Commons Codec – commons-codec Spring Security Crypto – spring-security-crypto Bitwise shifting and masking. (educational purposes) Note Both Apache Commons-Codec and Spring Security Crypto modules are using the similar 5. Bitwise …

Read more

How to convert InputStream to String in Java

This article shows a few ways to convert an java.io.InputStream to a String. Table of contents 1. ByteArrayOutputStream 2. InputStream#readAllBytes (Java 9) 3. InputStreamReader + StringBuilder 4. InputStreamReader + BufferedReader (modified line breaks) 5. Java 8 BufferedReader#lines (modified line breaks) 6. Apache Commons IO 7. Download Source Code 8. References What are modified line breaks? …

Read more

How to convert String to InputStream in Java

In Java, we can use ByteArrayInputStream to convert a String to an InputStream. String str = "mkyong.com"; InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)); Table of contents 1. ByteArrayInputStream 2. Apache Commons IO – IOUtils 3. Download Source Code 4. References 1. ByteArrayInputStream This example uses ByteArrayInputStream to convert a String to an InputStream and saves it …

Read more

Java – How to save byte[] to a file

This article shows a few ways to save a byte[] into a file. For JDK 1.7 and above, the NIO Files.write is the simplest solution to save byte[] to a file. // bytes = byte[] Path path = Paths.get("/path/file"); Files.write(path, bytes); FileOutputStream is the best alternative. try (FileOutputStream fos = new FileOutputStream("/path/file")) { fos.write(bytes); //fos.close …

Read more

Java – How to convert File to byte[]

In Java, we can use Files.readAllBytes(path) to convert a File object into a byte[]. import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; String filePath = "/path/to/file"; // file to byte[], Path byte[] bytes = Files.readAllBytes(Paths.get(filePath)); // file to byte[], File -> Path File file = new File(filePath); byte[] bytes = Files.readAllBytes(file.toPath()); P.S The NIO Files class is …

Read more

How to convert BufferedImage to byte[] in Java

This article shows how to convert a BufferedImage to a byte array or byte[]. BufferedImage bi = ImageIO.read(new File("c:\\image\\mypic.jpg")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", baos); byte[] bytes = baos.toByteArray(); The idea is uses the ImageIO.write to write the BufferedImage object into a ByteArrayOutputStream object, and we can get the byte[] from the ByteArrayOutputStream. …

Read more

How to convert String to byte[] in Java?

In Java, we can use str.getBytes(StandardCharsets.UTF_8) to convert a String into a byte[]. String str = "This is a String"; // default charset, a bit dangerous byte[] output1 = str.getBytes(); // in old days, before java 1.7 byte[] output2 = str.getBytes(Charset.forName("UTF-8")); // the best , java 1.7+ , new class StandardCharsets byte[] output3 = str.getBytes(StandardCharsets.UTF_8); …

Read more

How to convert byte[] array to String in Java

In Java, we can use new String(bytes, StandardCharsets.UTF_8) to convert a byte[] to a String. // string to byte[] byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8); // byte[] to string String s = new String(bytes, StandardCharsets.UTF_8); Table of contents 1. byte[] in text and binary data 2. Convert byte[] to String (text data) 3. Convert byte[] to String …

Read more