0% found this document useful (0 votes)
0 views

Experiment 10h

Uploaded by

selanawalahamza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Experiment 10h

Uploaded by

selanawalahamza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 10

Aim: To study the concept of file handling.

Problem Statement:
You have been given the list of the names of the files in a directory. You have to select
Java files from them. A file is a Java file if it’s name ends with “.java”. For e.g. File-
“Names.java” is a Java file, “FileNames.java.pdf” is not.
Input: test.java, ABC.doc, Demo.pdf, add.java, factorial.java, sum.txt.
Output: add.java, factorial.java, test.java.

Objective: Input Output streams in various applications.

Theory: File Handling: File Handling is an integral part of any programming language
as file handling enables us to store the output of any particular program in a file and
allows us to perform certain operations on it. In simple words, file handling means
reading and writing data to a file.
FileClass: In Java, with the help of File Class, we can work with files. This File Class is
inside the java.io package. It can be used by creating an object of the class and then
specifying the name of the file. It also has several methods for working with directories
and files such as creating new directories or files, deleting and renaming directories or
files, listing the contents of a directory etc.
Example: Let’s take a scenario where we need to list all Java source files in a folder, an
anonymous class implements FilenameFilter and overrides accept to include only .java
files, filtering out others for easier identification.
Some useful methods are:
createNewFile(): Creates a new empty file if it does not already exist.
canWrite(): Checks if the application can modify the file.
canExecute(): Checks if the application can execute the file.
canRead(): Checks if the application can read the file.
isDirectory(): Checks if the pathname is a directory.
isFile(): Checks if the pathname is a normal file.
list(FilenameFilter filter): Returns an array of file and directory names that match the
specified filter.
listFiles(): Returns an array of File objects representing the files and directories in the
specified directory.
These methods are crucial for file creation, access control, and filtering file lists.
Outcome: Construct robust and faster programmed solutions to problems using
concept of file handling.

Conclusion: This experiment helps the students to understand the concept of file
handling.

Code:
Output:

You might also like