Saturday, July 9, 2011

Viewing txt files on command prompt in Java

import java.io.*;

public class File1
{
public static void main (String[] args)
{
String file_name;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name eg: (d:\\test.txt) ");
try
{
file_name = br.readLine(); // getting file name from user
InputStream fileIn = new FileInputStream(file_name);
File ff = new File(file_name);
long lon_size = ff.length(); //getiing the length of the file
int buff_size = (int)lon_size;
System.out.println("Size of File " + lon_size);
byte buff[] = new byte[buff_size];
int i = fileIn.read(buff);
String s = new String(buff);
System.out.println(s);
}

catch(FileNotFoundException e)
{
System.out.println("File not found.");
}

catch(IOException e)
{
e.printStackTrace();
}

} //main
}//class

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More