/*
Write a Java program to accept a string from user and check whether it is a file or a directory. If it is a directory, display the files in the directory. If it is a file, display the length of the file in bytes.
*/
import java.io.*;
class FileHandling
{
public static void main(String as[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Enter string : “);
String str = br.readLine().trim();
File f1 = new File(str);
if (f1.isFile()==true)
System.out.println(“File length = “+f1.length() + ” bytes”);
else if(f1.isDirectory()==true)
{
String s[] = f1.list();
System.out.println(“Files in given directory:”);
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
else
System.out.println(“Not a file or dir”);
}
}
/*
Sample Output:
D:\java\iostreams>javac FileHandling.java
D:\java\iostreams>java FileHandling
Enter string : D:/java/iostreams/FileHandling.java
File length = 863 bytes
D:\java\iostreams>java FileHandling
Enter string : D:/java/iostreams
Files in given directory:
FileHandling.class
FileHandling.java
D:\java\iostreams>java FileHandling
Enter string : asdf
Not a file or dir
*/