- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write contents of a file to byte array in Java?
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.
Example
import java.io.File; import java.io.FileInputStream; public class FileToByteArray { public static void main(String args[]) throws Exception { File file = new File("HelloWorld"); 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
//Class declaration public class SampleProgram { /* This is my first java program. This will print 'Hello World' as the output */ //Main method public static void main(String []args) { //prints Hello World System.out.println("Hello World"); } }
- Related Articles
- Java Program to Convert contents of a file to byte array and Vice-Versa
- How to print a byte array in Java?
- How to store the contents of arrays in a file using Java?
- How to create and write JSON array to a file in java?
- How to read the contents of a JSON file using Java?
- Java Program to write int array to a file
- How to concatenate byte array in java?
- How to convert a PDF to byte array in Java?
- Java Program to write an array of strings to a file
- How to overwrite a file to hide file contents, and make original contents unrecoverable in Linux?
- How to get the Checksum of a Byte Array in Java?
- Java Program to Create String from Contents of a File
- How to convert BLOB to Byte Array in java?
- How to convert Byte Array to Image in java?
- How to convert Image to Byte Array in java?

Advertisements