JAVA THEORY PPT
NAME : K.Vivek Vardhan
ROLL NO : 237R1A05V0
TOPIC : File class, Reading and Writing Files, Random access
files.
INTRODUCTION:
FILES:
• The File is a built-in class in Java.
In java, the File class has been defined in the java.io package.
• The File class has various methods to perform operations like
creating a file or directory,
reading from a file,
updating file content,
deleting a file or directory.
The File class in java has the following constructors
S.No. Constructor with Description
1 File(String pathname)
2 File(String parent, String child)
3 File(File parent, String child)
4 File(URI uri)
Example:
import java.io.*;
public class FileClassTest { public static void main(String args[]) {
File f = new File("C:\\Raja\\datFile.txt");
System.out.println("Executable File : " + f.canExecute());
System.out.println("Name of the file : " + f.getName());
System.out.println("Path of the file : " + f.getAbsolutePath());
System.out.println("Parent name : " + f.getParent());
System.out.println("Write mode : " + f.canWrite());
System.out.println("Read mode : " + f.canRead());
System.out.println("Existance : " + f.exists());
System.out.println("Last Modified : " + f.lastModified());
System.out.println("Length : " + f.length());
//f.createNewFile()
//f.delete();
//f.setReadOnly()
}
}
File Reading & Writing in Java
In java, there multiple ways to read data from a file and to write data to a file. The
most commonly used ways are as follows.
Using Byte Stream (FileInputStream and FileOutputStream)
Work with raw binary data (8-bit bytes). This includes all types of files like
images, audio, and video files, or any non-text data.
Character Stream
(FileReader and FileWriter)(BufferedReader and
BufferedWriter)
Work with character data (16-bit Unicode characters). They are specifically designed for
reading and writing text files (i.e., files containing human-readable text).
Example”
import java.io.*;
public class FileReadingTest {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\Raja\\Input-File.txt");
out = new FileOutputStream("C:\\Raja\\Output-File.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
System.out.println("Reading and Writing has been success!!!");
}
catch(Exception e){
System.out.println(e);
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close(); } } }
}
Random access files
In Java, Random Access Files allow you to read from and
write to a file at any position, meaning you can jump to any
location in the file without having to read through the entire
file sequentially. This is particularly useful when you want to
modify or read specific parts of a file without having to
process the whole file. Random access files are part of the
java.io package and are handled using the
RandomAccessFile class.
Key Features of Random Access File
• Allows both reading and writing to a file.
• Supports seeking (moving the file pointer to any position).
• Works with both text (character) and binary data.
CONCLUSION:
File handling is a critical aspect of Java programming, enabling developers to
manage, read, and manipulate data stored in files effectively. Java provides
robust classes and methods to work with files in different scenarios, including
File class, stream-based file I/O, and random access file operations.