Venkatesh-Java File concept


JAVA FILE CONCEPT



1) Write a program to create a new file using File class.


Code :

import java.io.File;

import java.io.IOException;


class createfileEx

 {

    public static void main(String[] args) 

    {

        System.out.println("Question 1: Create a New File using File class.");

        try

         {

            File venkatfile = new File("sample.txt");

            if (venkatfile.createNewFile()) 

            {

                System.out.println("File created: " + venkatfile.getName());

            } else 

            {

                System.out.println("File already exists.");

            }

        } catch (IOException e)

         {

            e.printStackTrace();

        }

    }

}


Output :




2) Write a program to check if a file exists.


Code :

import java.io.File;


class CheckFileExist {

    public static void main(String[] args) {

        System.out.println("Question 2: Check if a File Exists.");

        File file = new File("sample.txt");

        if (file.exists()) {

            System.out.println("File exists.");

        } else {

            System.out.println("File does not exist.");

        }

    }

}


Output :







3) Write a program to write text into a file using FileWriter.


Code :

import java.io.FileWriter;

import java.io.IOException;


class WriteFileExample {

    public static void main(String[] args) {

        System.out.println("Question 3: Write Text into a File using FileWriter.");

        try {

            FileWriter writer = new FileWriter("sample.txt");

            writer.write("Hello, this is a test file.\nWelcome to Java File Handling.");

            writer.close();

            System.out.println("Successfully wrote to the file.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


Output :






4) Write a program to read text from a file using FileReader.


Code :

import java.io.FileReader;

import java.io.IOException;


class ReadFileExample {

    public static void main(String[] args) {

        System.out.println("Question 4: Read Text from a File using FileReader.");

        try {

            FileReader reader = new FileReader("sample.txt");

            int ch;

            while ((ch = reader.read()) != -1) {

                System.out.print((char) ch);

            }

            reader.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


Output :






5) Write a program to read text from a file line by line using BufferedReader.


Code :

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;


class ReadLineExample {

    public static void main(String[] args) {

        System.out.println("Question 5: Read File Line by Line using BufferedReader.");

        

        try {

            BufferedReader br = new BufferedReader(new FileReader("sample.txt"));

            String line;

            while ((line = br.readLine()) != null) {

                System.out.println(line);

            }

            br.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


Output :






6) Write a program to append text to a file without overwriting.


Code :

import java.io.FileWriter;

import java.io.IOException;


class AppendFileExample {

    public static void main(String[] args) {

        System.out.println("Question 6: Append Text to a File without Overwriting.");

        

        try {

            FileWriter writer = new FileWriter("sample.txt", true);

            writer.write("\nThis line is appended.");

            writer.close();

            System.out.println("Text appended successfully.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


Output :






7) Write a program to count number of words in a text file.


v

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;


class WordCountExample {

    public static void main(String[] args) {

        System.out.println("Question 7:  Count Number of Words in a File.");

        int wordCount = 0;

        try {

            BufferedReader br = new BufferedReader(new FileReader("sample.txt"));

            String line;

            while ((line = br.readLine()) != null) {

                String[] words = line.split("\\s+");

                wordCount += words.length;

            }

            br.close();

            System.out.println("Total words: " + wordCount);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


Output :






8) Write a program to count number of lines in a file.


Code :

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;


class LineCountExample {

    public static void main(String[] args) {

        System.out.println("Question 8: Count Number of Lines in a File.");

        

        int lineCount = 0;

        try {

            BufferedReader br = new BufferedReader(new FileReader("sample.txt"));

            while (br.readLine() != null) {

                lineCount++;

            }

            br.close();

            System.out.println("Total lines: " + lineCount);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


Output :






9) Write a program to copy contents from one file to another.


Code :

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;


class CopyFileExample {

    public static void main(String[] args) {

        System.out.println("Question 9: Copy Contents from One File to Another.");

        

        try {

            FileReader fr = new FileReader("sample.txt");

            FileWriter fw = new FileWriter("copy.txt");

            int ch;

            while ((ch = fr.read()) != -1) {

                fw.write(ch);

            }

            fr.close();

            fw.close();

            System.out.println("File copied successfully.");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


Output :






10) Write a program to serialize and deserialize an object (save and read object from file).


Code :

import java.io.*;


class Student implements Serializable {

    String name;

    int age;

    public Student(String name, int age) {

        this.name = name;

        this.age = age;

    }

}


class SerializationExample {

    public static void main(String[] args) {

        System.out.println("Question 10: Serialize and Deserialize an Object.");

        

        String filename = "student.ser";


        // Serialize

        try {

            Student s1 = new Student("John", 21);

            FileOutputStream fos = new FileOutputStream(filename);

            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(s1);

            oos.close();

            fos.close();

            System.out.println("Object serialized successfully.");

        } catch (IOException e) {

            e.printStackTrace();

        }


        // Deserialize

        try {

            FileInputStream fis = new FileInputStream(filename);

            ObjectInputStream ois = new ObjectInputStream(fis);

            Student s2 = (Student) ois.readObject();

            ois.close();

            fis.close();

            System.out.println("Object deserialized successfully.");

            System.out.println("Name: " + s2.name + ", Age: " + s2.age);

        } catch (IOException | ClassNotFoundException e) {

            e.printStackTrace();

        }

    }

}


Output :






Comments

Popular posts from this blog