Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Reading a File into a Byte Array

                         

In this program we will read a file in the form of a byte array. We can do it very easily. Firstly we will create an object of File class and pass the name of the file which we want to read, in the constructor of File class. We will use DataInputStream to read primitive data types.

We are using the following methods:

length(): It gives the length of the file.

 

 

Getting the file size code: ReadInByteArray.java

import java.io.*;

public class ReadInByteArray 
{
  public static void main(String[] args
  {
    try
    {
      File file = new File("tapan.txt")
      int size = (int)file.length();    //It gives the length of the file. 
 
    System.out.println("Size of the file: " + size);
    }
    catch (Exception e)
    {
      System.out.println("Exception has been thrown :" + e);
    }
  }
}

Download this program

The following code helps you in getting the file size in byte array.

 Here is the Byte Array code: ByteArrayExample.java

import java.io.*;

public class ByteArrayExample{
  public static void main(String[] args) {
    try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter File name: ");
      String str = bf.readLine();
      File file = new File(str)
      //File length
      int size = (int)file.length()
      if (size > Integer.MAX_VALUE){
        System.out.println("File is to larger");
      }
      byte[] bytes = new byte[size]
      DataInputStream dis = new DataInputStream(new FileInputStream(file))
      int read = 0;
      int numRead = 0;
      while (read < bytes.length && (numRead=dis.read(bytes, read,
                                                bytes.length-read
)) >= 0) {
        read = read + numRead;
      }
      System.out.println("File size: " + read);
      // Ensure all the bytes have been read in
      if (read < bytes.length) {
        System.out.println("Could not completely read: "+file.getName());
      }
    }
    catch (Exception e){
      e.getMessage();
    }
  }
}

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

This reading in byte code program is nicely done. It helped me out a lot.

Doug

Posted by doug on Sunday, 09.28.08 @ 03:09am | #80744

Latest Searches:
struts2 file upload
compression of a file
How to show expression
Flash Special Effects
Photoshop Photo Effect
Database MS Access Dat
Tutorial:FlashBasicPas
insert data to text fi
passing value from ser
adding scrollbar in Jp
jsp Insert data in mys
Photoshop Textures and
Selection Sort In Java
Photoshop Digital Art
Exercise - Pad Left
Set hashCode
Tomcat deploy JSP
How to create Jasperpr
transientinjava
netbeans jsp
Catching Exception usi
convert Time to String
count the number of di
Determining If a Year
netbeans jsp jdbc
jsp bean get property
array
Photoshop Textures and
delete data to text fi
how to disable the tex
jta
split
covert string to ident
passing values from js
getting xml sub elemen
addition of two number
customise css in jsp
JScrollPane
Struts HTML EL tags
covert uppercase to lo
convert linked list to
<s:a
requestdispatcher() in
timestamp
split string
send email
crud application using
pb tree implementati
poi-bin-3.1-beta2-200
AND Operator
prefix=
wap to print a trriang
Web Focus
string buffer in java
decimal to binary conv
ajax auto complete
sort user input number
check a string for spa
write int to file
jsp select
Photoshop Photo Retouc
generic
xml to Excel using ja
spring jsf integration
Janino -- an Embedded
ASP Database Related C
Arrays
Why Virtual Dedicated
jboss connection pool
PHP Date and Time Last
invalidate()ethode
FCKediter
how to write in pdf us
struts menu
Initialization
Combo Box operation in
binary recursio
updata into csv
horizontal curved lin
Dojo Progress Bar
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.