Wednesday, December 22, 2010

Java-Reading a file


import java.io.*;

public class ShowFile {
  public static void main(String args[]) throws IOException {
    if (args.length != 0) {
      File file = new File(args[0]);
      if (file.exists() && file.isFile()) {
        FileInputStream input_file =
                           new FileInputStream(file);
        /*FileInputStream input_file =
                     new FileInputStream(args[0]);*/
        int file_size = input_file.available();
        byte[] data = new byte[file_size];
        input_file.read(data);
        System.out.println(new String(data));
        input_file.close();
      } else {
        System.out.println("\"" + args[0] +
                           "\" not found !!!");
                }
    } else {
      System.out.println("Error in usage:");
      System.out.println(
                     "  Usage - java ShowFile ");
           }
  }
}

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *