

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Convert contents of a file to byte array and Vice-Versa
The FileInputStream class contains a method read(), this method accepts a byte array as a parameter and it reads the data of the file input stream to given byte array. Assume the file myData contains the following data −
Example
import java.io.File; import java.io.FileInputStream; public class FileToByteArray { public static void main(String args[]) throws Exception{ File file = new File("myData"); FileInputStream fis = new FileInputStream(file); byte[] bytesArray = new byte[(int)file.length()]; fis.read(bytesArray); String s = new String(bytesArray); System.out.println(s); } }
Output
Hi how are you welcome to Tutorialspoint
- Related Questions & Answers
- Java Program to Convert the LinkedList into an Array and vice versa
- Java Program to Convert the ArrayList into a string and vice versa
- How to convert an array to Set and vice versa in Java?
- How to convert String to StringBuilder and vice versa Java?
- How to write contents of a file to byte array in Java?
- C++ Program to Convert Binary Number to Decimal and vice-versa
- C++ Program to convert Octal Number to Decimal and vice-versa
- C++ Program to Convert Binary Number to Octal and vice-versa
- How to Convert a String to Hexadecimal and vice versa format in java?
- C++ Program to Convert Km/hr to miles/hr and vice versa
- Convert string to DateTime and vice-versa in Python
- How to convert a NumPy ndarray to a PyTorch Tensor and vice versa?
- C Program for LowerCase to UpperCase and vice-versa
- Converting Strings to Numbers and Vice Versa in Java.
- Java Program to convert byte[] array to String
Advertisements