0% found this document useful (0 votes)
4 views3 pages

File Programs

The document contains two Java classes for file handling. The first class, FileHandling, prompts the user for a file name and checks its existence, readability, writability, and other properties. The second class, FileDemo, reads a specified text file and counts the number of characters, words, and lines, handling potential file-related exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

File Programs

The document contains two Java classes for file handling. The first class, FileHandling, prompts the user for a file name and checks its existence, readability, writability, and other properties. The second class, FileDemo, reads a specified text file and counts the number of characters, words, and lines, handling potential file-related exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

*;
import java.util.*;
public class FileHandling {

public static String getFileExtension(File file) {


String name = file.getName();
int lastIndexOf = name.lastIndexOf(".");
if (lastIndexOf == -1) {
return ""; // empty extension
}
return name.substring(lastIndexOf);
}

public static void main(String[] args) {


System.out.println("Enter the file name");
Scanner in = new Scanner(System.in);
String s = in.nextLine();
File f1= new File(s);
System.out.println("------------------------");
System.out.println("File name: "+f1.getName());
System.out.println(f1.exists()?"exists":"Does not exist");
if(f1.exists())
{
if(f1.canRead()&&f1.canWrite())
{
System.out.println("File is both Readable and Writable.");
}
else if(f1.canRead())
{
System.out.println("Readable: "+f1.canRead());
}
else if(f1.canWrite())
{
System.out.println("Writable: "+f1.canWrite());
}
System.out.println("Path: "+f1.getPath());
System.out.println("Absolute path name: "+f1.getAbsolutePath());
System.out.println("is file: "+f1.isFile());
System.out.println("is directory: "+f1.isDirectory());
System.out.println("File length: "+f1.length()+" bytes");
System.out.println("Hidden file: "+f1.isHidden());
System.out.println("File type:"+getFileExtension(f1));

}
}
}

***
import java.io.*;
class FileDemo
{
public static void main(String args[])
{
try
{
int lines=0,chars=0,words=0;
int code=0;
FileInputStream fis = new FileInputStream("sample.txt");
while(fis.available()!=0)
{
code = fis.read();
if(code!=10)
chars++;
if(code==32)
words++;
if(code==13)
{
lines++;
words++;
}
}
System.out.println("No.of characters = "+chars);
System.out.println("No.of words = "+(words));
System.out.println("No.of lines = "+(lines));
fis.close();
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find the specified file...");
}
catch(IOException i)
{
System.out.println("Cannot read file...");
}
}
}

You might also like