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();}}
Java File to Byte Array Example
Subscribe to:
Post Comments (Atom)
Model Context Protocol (MCP) — Complete Guide for Backend Engineers
Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...
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...
-
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...
-
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...
-
Camel Timer component is used to generate or process message exchanges when a time fires. Here is one example to print ‘Hello world ‘ in eac...
-
🔢 Java Sorting Algorithms — Step-by-Step with Iterations Sorting is a fundamental concept in programming and data structures. Below are t...
No comments:
Post a Comment