ASS5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

12202040501019 Dharmik Rabadiya

Assignment 5

1]Program to copy contents of multiple files into a single file:

import java.io.*;

public class CopyFiles {

public static void main(String[] args) throws IOException {

if (args.length < 2) {

System.out.println("Usage: java CopyFiles <destination_file> <source_file1> <source_file2> ...");

return;

File destination = new File(args[0]);

try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destination))) {

for (int i = 1; i < args.length; i++) {

try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(args[i]))) {

int b;

while ((b = in.read()) != -1) {

out.write(b);

System.out.println("Files copied successfully!");

Output:

$ java CopyFiles destination.txt source1.txt source2.txt

Files copied successfully!

2]Program to count the number of letters, words, and lines in a file:

import java.io.*;

public class FileStatistics {


12202040501019 Dharmik Rabadiya

public static void main(String[] args) throws IOException {

if (args.length < 1) {

System.out.println("Usage: java FileStatistics <filename>");

return;

File file = new File(args[0]);

int letters = 0, words = 0, lines = 0;

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

String line;

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

lines++;

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

words += wordsArray.length;

for (String word : wordsArray) {

letters += word.length();

System.out.println("Number of letters: " + letters);

System.out.println("Number of words: " + words);

System.out.println("Number of lines: " + lines);

Output:

$ java FileStatistics sample.txt

Number of letters: 70

Number of words: 14

Number of lines: 3

For the rest of the programs, let me add the output statements.
12202040501019 Dharmik Rabadiya

3]Program to write student details into a file and display details of students who stay in AHMEDABAD:

import java.io.*;

import java.util.*;

class Student implements Serializable {

String name;

int age;

String gender;

String city;

String mobNo;

public Student(String name, int age, String gender, String city, String mobNo) {

this.name = name;

this.age = age;

this.gender = gender;

this.city = city;

this.mobNo = mobNo;

@Override

public String toString() {

return "Name: " + name + ", Age: " + age + ", Gender: " + gender + ", City: " + city + ", Mobile No: " + mobNo;

public class StudentDetails {

public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();

students.add(new Student("John", 20, "Male", "AHMEDABAD", "1234567890"));

students.add(new Student("Alice", 22, "Female", "MUMBAI", "9876543210"));

students.add(new Student("Bob", 21, "Male", "AHMEDABAD", "5678901234"));

// Write student details to file

try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student_details.dat"))) {

for (Student student : students) {


12202040501019 Dharmik Rabadiya

out.writeObject(student);

} catch (IOException e) {

e.printStackTrace();

System.out.println("Student details saved successfully.");

// Display details of students who stay in AHMEDABAD

System.out.println("\nStudents staying in AHMEDABAD:");

try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("student_details.dat"))) {

while (true) {

Student student = (Student) in.readObject();

if (student.city.equals("AHMEDABAD")) {

System.out.println(student);

} catch (EOFException e) {

// End of file reached

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

Output:

Student details saved successfully.

Students staying in AHMEDABAD:

Name: John, Age: 20, Gender: Male, City: AHMEDABAD, Mobile No: 1234567890

Name: Bob, Age: 21, Gender: Male, City: AHMEDABAD, Mobile No: 5678901234

4]Program that takes filename and word as input and displays the occurrence of that word in a file:
12202040501019 Dharmik Rabadiya

import java.io.*;

public class WordOccurrences {

public static void main(String[] args) throws IOException {

if (args.length < 2) {

System.out.println("Usage: java WordOccurrences <filename> <word>");

return;

File file = new File(args[0]);

String word = args[1];

int count = 0;

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

String line;

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

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

for (String w : words) {

if (w.equals(word)) {

count++;

System.out.println("Occurrences of '" + word + "' in " + args[0] + ": " + count);

Output:

$ java WordOccurrences sample.txt Java

Occurrences of 'Java' in sample.txt: 2

Let me know if you need further assistance!


12202040501019 Dharmik Rabadiya

5]Program to display all files of a directory and its subdirectories:

import java.io.*;

public class FileList {

public static void main(String[] args) {

if (args.length < 1) {

System.out.println("Usage: java FileList <directory>");

return;

File directory = new File(args[0]);

listFiles(directory);

public static void listFiles(File directory) {

File[] files = directory.listFiles();

if (files != null) {

for (File file : files) {

if (file.isDirectory()) {

listFiles(file);

} else {

System.out.println(file.getAbsolutePath());

Output:

$ java FileList /path/to/directory

/path/to/directory/file1.txt

/path/to/directory/subdirectory/file2.txt

/path/to/directory/subdirectory/subsubdirectory/file3.txt
12202040501019 Dharmik Rabadiya

6]Program to calculate the average temperature for every year from a file containing year, month, day, and temperature
separated by commas:

import java.io.*;

import java.util.*;

public class AverageTemperature {

public static void main(String[] args) throws IOException {

if (args.length < 1) {

System.out.println("Usage: java AverageTemperature <filename>");

return;

File file = new File(args[0]);

HashMap<Integer, ArrayList<Double>> yearTemperatures = new HashMap<>();

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {

String line;

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

String[] parts = line.split(",");

int year = Integer.parseInt(parts[0]);

double temp = Double.parseDouble(parts[3]);

if (!yearTemperatures.containsKey(year)) {

yearTemperatures.put(year, new ArrayList<>());

yearTemperatures.get(year).add(temp);

} for (Map.Entry<Integer, ArrayList<Double>> entry : yearTemperatures.entrySet()) {

int year = entry.getKey();

ArrayList<Double> temps = entry.getValue();

double sum = 0;

for (double temp : temps) {


12202040501019 Dharmik Rabadiya

sum += temp;

double average = sum / temps.size();

System.out.println("Average temperature for year " + year + ": " + average);

Output:

$ java AverageTemperature temperatures.csv

Average temperature for year 2022: 15.5

Average temperature for year 2023: 17.3

You might also like