package com.pretech;import java.io.*;public class FileToByteArray {public static void main(String[] args) {try {File file1 = new File("filetest.txt");readFile(file1);byte[] myarray = fileToByteArray(file1);File file2 = byteArrayToFile(myarray);readFile(file2);} catch (Exception e) {e.printStackTrace();}}/*** Method to convert file to byte array.** @param file* @return* @throws IOException*/public static byte[] fileToByteArray(File file) throws IOException {ByteArrayOutputStream buffer = new ByteArrayOutputStream();InputStream inputStream = new FileInputStream(file);int nRead;byte[] data = new byte[16384];while ((nRead = inputStream.read(data, 0, data.length)) != -1) {buffer.write(data, 0, nRead);}buffer.flush();return buffer.toByteArray();}/*** Method to convert byte array to file** @param bytearray* @return* @throws IOException*/public static File byteArrayToFile(byte[] bytearray) throws IOException {File file = new File("test.txt");file.createNewFile();FileOutputStream fos = new FileOutputStream(file);fos.write(bytearray);fos.close();return file;}/*** Reading a file.** @param file* @throws IOException*/public static void readFile(File file) throws IOException {FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);String s;while ((s = br.readLine()) != null) {System.out.println(s);}fr.close();}}
Keep Traveling
Java Byte Array to File Example
Subscribe to:
Post Comments (Atom)
Featured Post
Confusion Matrix + Precision/Recall (Super Simple, With Examples)
Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...
Featured Posts
-
In this example we will see how do to the file encryption and decryption using Apache Camel using pgp 1. Generate pgp keys In order to do t...
-
Spring provides a JMS integration framework that simplifies the use of the JMS API, the JmsTemplate class is the core class which is availab...
-
The selective consumer is consumer that applies a filter to incoming messages so that only messages meeting that specific selection criteria...
-
Apache camel API has the inbuilt kafka component and it is very simple to create producer, consumer and process messages. Here is one simple...
-
Apache camel provides intercepting feature while Exchanges are on route. Camel supports three types for interceptors ( See more about Camel ...
-
Maven Error Notes [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on projec...
-
☸️ Helm vs Kubectl — What, Why, and When to Use Each If you work with Kubernetes, you will almost always use both kubectl and Helm — bu...
-
In the previous example ( Mocking Static methods ) we created mock values for Static methods, in this example we will see how to mock new in...
-
🔢 Java Sorting Algorithms — Step-by-Step with Iterations Sorting is a fundamental concept in programming and data structures. Below are t...
-
Itext PDF is an open source API that allows to create and modify pdf documents in java. In this example we will see how to create and and i...
No comments:
Post a Comment