0% found this document useful (0 votes)
33 views5 pages

Write A Java Program To List All The Files in A Directory Including The Files

Uploaded by

sanjaygoud61065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Write A Java Program To List All The Files in A Directory Including The Files

Uploaded by

sanjaygoud61065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1) Write a Java program to list all the files in a directory including the files

SOURCE CODE:

import java.io.File;

public class ListFilesInDirectory {

public static void main(String[] args) {

// Specify the directory path here

String directoryPath = "path/to/your/directory";

File directory = new File(directoryPath);

if (directory.exists() && directory.isDirectory()) {

System.out.println("Listing files in directory: " + directoryPath);

listFiles(directory);

} else {

System.out.println("The specified path is not a directory or does not exist.");

// Method to list files in a directory and subdirectories

public static void listFiles(File dir) {

// Get all files and directories in the current directory

File[] files = dir.listFiles();

if (files != null) { // Check to ensure the directory can be read

for (File file : files) {

if (file.isFile()) {
System.out.println("File: " + file.getAbsolutePath());

} else if (file.isDirectory()) {

System.out.println("Directory: " + file.getAbsolutePath());

// Recursive call to list files in the subdirectory

listFiles(file);

2. Write a Java program to list all the files in a directory including the files present in all its
subdirectories.

SOURCE CODE:

import java.io.File;

public class ListFilesRecursive {

public static void main(String[] args) {

// Specify the directory path

String directoryPath = "path/to/your/directory";

File directory = new File(directoryPath);

if (directory.exists() && directory.isDirectory()) {

System.out.println("Listing all files in directory and subdirectories: " + directoryPath);

listFiles(directory);

} else {
System.out.println("The specified path is not a directory or does not exist.");

// Method to list all files in a directory and its subdirectories

public static void listFiles(File dir) {

// Get all files and directories in the current directory

File[] files = dir.listFiles();

if (files != null) { // Check if directory can be read

for (File file : files) {

if (file.isFile()) {

System.out.println("File: " + file.getAbsolutePath());

} else if (file.isDirectory()) {

System.out.println("Directory: " + file.getAbsolutePath());

// Recursive call to list files in the subdirectory

listFiles(file);

3. Write a Java program on Random Access File class to perform different read and write operations.

SOURCE CODE:
import java.io.IOException;

import java.io.RandomAccessFile;

public class RandomAccessFileDemo {

public static void main(String[] args) {

String filePath = "example.txt";

try {

// Create a new RandomAccessFile in read-write mode

RandomAccessFile raf = new RandomAccessFile(filePath, "rw");

// Write some data to the file

raf.writeUTF("Hello, RandomAccessFile!");

raf.writeInt(42);

raf.writeDouble(3.14159);

raf.writeBoolean(true);

// Move the file pointer to the beginning of the file for reading

raf.seek(0);

// Read data from the file in the same order it was written

System.out.println("String: " + raf.readUTF());

System.out.println("Integer: " + raf.readInt());

System.out.println("Double: " + raf.readDouble());

System.out.println("Boolean: " + raf.readBoolean());


// Modify specific data in the file

raf.seek(raf.getFilePointer() - 9); // Move pointer to the beginning of the Double data

raf.writeDouble(2.71828); // Replace 3.14159 with 2.71828

// Move the pointer back to the beginning of the Double data and read again

raf.seek(raf.getFilePointer() - 8);

System.out.println("Modified Double: " + raf.readDouble());

// Close the file

raf.close();

} catch (IOException e) {

System.out.println("An I/O error occurred: " + e.getMessage());

You might also like