Jump to Real's How-to Main page

Read a file into a variable in one shot

import java.io.*;

public class ReadFullyIntoVar {
   public static void main(String argv[]){
     try {
       FileInputStream file = new FileInputStream (argv[0]);
       DataInputStream in = new DataInputStream (file);
       byte[] b = new byte[in.available ()];
       in.readFully (b);
       in.close ();
       String result = new String (b, 0, b.length, "Cp850");
       /* */
       System.out.println(result);
       }
     catch (Exception e) {
       e.printStackTrace();
       }
     }
}

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]