0% found this document useful (0 votes)
3 views4 pages

Java File Handling MCA Notes & Programs

java file handling

Uploaded by

omkar K
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)
3 views4 pages

Java File Handling MCA Notes & Programs

java file handling

Uploaded by

omkar K
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

Complete File Handling in Java - MCA Level Notes

+ Programs
This PDF contains all important Java File Handling concepts and full programs using File,
FileReader, FileWriter, BufferedReader, BufferedWriter, FileInputStream, FileOutputStream, and
file copy operations.

1. Create a File
import [Link];
import [Link];

public class CreateFile {


public static void main(String[] args) {
try {
File f = new File("[Link]");
if ([Link]()) {
[Link]("File created: " + [Link]());
} else {
[Link]("File already exists.");
}
} catch (IOException e) {
[Link]("An error occurred.");
}
}
}

2. Write to a File (FileWriter)


import [Link];
import [Link];

public class WriteFile {


public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("[Link]");
[Link]("Hello MCA Students!\nThis is file handling.");
[Link]();
[Link]("Data written.");
} catch (IOException e) {
[Link]("Error writing file.");
}
}
}

3. Read from a File (FileReader)


import [Link];
import [Link];

public class ReadFile {


public static void main(String[] args) {
try {
FileReader reader = new FileReader("[Link]");
int ch;
while ((ch = [Link]()) != -1) {
[Link]((char) ch);
}
[Link]();
} catch (IOException e) {
[Link]("Error reading file.");
}
}
}

4. Read Line-by-Line (BufferedReader)


import [Link];
import [Link];

public class ReadLineByLine {


public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (Exception e) {
[Link]("Error!");
}
}
}

5. Append to a File
import [Link];

public class AppendFile {


public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("[Link]", true);
[Link]("\nAppending using FileWriter.");
[Link]();
[Link]("Data appended.");
} catch (Exception e) {
[Link]("Error!");
}
}
}

6. Delete a File
import [Link];

public class DeleteFile {


public static void main(String[] args) {
File f = new File("[Link]");
if ([Link]()) {
[Link]("File deleted.");
} else {
[Link]("Delete failed.");
}
}
}

7. FileInputStream (Read Bytes)


import [Link];

public class ReadUsingFIS {


public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("[Link]");
int ch;
while ((ch = [Link]()) != -1) {
[Link]((char) ch);
}
[Link]();
} catch (Exception e) {
[Link]("Error reading.");
}
}
}

8. FileOutputStream (Write Bytes)


import [Link];

public class WriteUsingFOS {


public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("[Link]");
String data = "Writing using FileOutputStream.";
[Link]([Link]());
[Link]();
[Link]("Written successfully.");
} catch (Exception e) {
[Link]("Error writing.");
}
}
}

9. Copy File using Streams


import [Link];
import [Link];

public class CopyFile {


public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("[Link]");
FileOutputStream fos = new FileOutputStream("[Link]");
int ch;
while ((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
[Link]("File copied.");
} catch (Exception e) {
[Link]("Error copying file.");
}
}
}

10. RandomAccessFile Example


import [Link];

public class RandomAccessExample {


public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("[Link]", "rw");
[Link](12345);
[Link](0);
[Link]([Link]());
[Link]();
}
}

Final Notes
This PDF includes all major file handling topics required for MCA exams. Use these programs
directly in practicals, assignments, and viva preparation.

You might also like