Tutorialspoint examples

Learn java tutorial with examples for beginners online

read file content in byte array java


Example:

package com.tutorialspointexamples;
 
import java.io.FileInputStream;
import java.io.InputStream;
 
public class FileToByteArray {
  public static void main(String args[]){
	 String fileName = "D:/Test files/file 3.txt";
     InputStream is = null;
     try {
         is = new FileInputStream(fileName);
         byte content[] = new byte[2*1024];
         int readCount = 0;
         while((readCount = is.read(content)) > 0){
             System.out.println(new String(content, 0, readCount-1));
         }
     } catch (Exception e) {
         e.printStackTrace();
     } 
  }
}

Output:

Content file

Download this example.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status