- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 read the data from a file in Java?
The read(byte[] b) method of the FileInputStream class reads up to the b.length from this file input stream to the array of bytes. The method blocks until some input is available.
Example
import java.io.File; import java.io.FileInputStream; public class Sample { public static void main(String args[]) throws Exception { int count =0; 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("Contents of the given file are :: " +new String(bytesArray)); } }
Output
Contents of the given file are :: Hi how are you welcome to tutorialspoint
- Related Articles
- How to read data from .csv file in Java?
- How to read the data from a properties file in Java?\n
- How to read the data from a CSV file in Java?\n
- How to read data in from a file to String using java?
- How to read/write data from/to .properties file in Java?
- How to read data from one file and print to another file in Java?
- How to read data from a file using FileInputStream?
- How to read data from PDF file and display on console in Java?
- How to read a 2d array from a file in java?
- How to read data from *.CSV file using JavaScript?
- How to read integers from a file using BufferedReader in Java?
- Read Data from a Text File using C++
- How to read certain number of elements from a file in Java?
- How to read data from all files in a directory using Java?
- How to create a file, write data into it and read data from it on iOS?

Advertisements