0% found this document useful (0 votes)
14 views

Java Examples

The document describes a Java program that can merge the contents of multiple text files into a single output file. It takes an array of file names as input and writes the merged content to a new output file, handling exceptions if any file is not found or there are input/output errors.

Uploaded by

Srivarshan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Examples

The document describes a Java program that can merge the contents of multiple text files into a single output file. It takes an array of file names as input and writes the merged content to a new output file, handling exceptions if any file is not found or there are input/output errors.

Uploaded by

Srivarshan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. program that merges the contents of multiple text files into a single text file.

Solution:-

package c1;

import java.io.*;

public class Excep {

public static void main(String[] args) {

File[] files = new File[3];

File file1 = new File("D:\\file1.txt");

File file2 = new File("D:\\file2.txt");

File file3 = new File("D:\\file3.txt");

files[0] = file1;

files[1] = file2;

files[2] = file3;

File merge = new File("D:\\merge.txt");

mergeFiles(files, merge);

private static void mergeFiles(File[] files, File merge) {

try (BufferedWriter writer = new BufferedWriter(new FileWriter(merge))) {

for (File fileName : files) {

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

String line = "";

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

writer.write(line);

writer.newLine();

writer.write("\n");

} catch (IOException e) {
System.err.println("Error reading file: " + fileName);

e.printStackTrace();

System.out.println("Files merged successfully into " + merge);

} catch (IOException e) {

System.err.println("Error creating merged file: " + merge);

e.printStackTrace();

Input:- multiple file names

Output:- merged file

2. write java program that reads the contents of a file and handles exceptions for file not found
and input-output errors.

Solution:-

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.io.FileNotFoundException;

public class Excep {

public static void main(String[] args) {

File file = new File("D:\\example.txt");

BufferedReader reader = null;

try {

reader = new BufferedReader(new FileReader(file));

String line;
while ((line = reader.readLine()) != null)

System.out.println(line);

} catch (FileNotFoundException e)

System.err.println("File not found: " + file);

} catch (IOException e)

System.err.println("Error reading the file: " + e.getMessage());

Input:- input file

Output:- data

3. Write a java code using object, class, inheritance, polymorphism, abstraction and
encapsulation.

Solution:-

class Teacher

String name;

int age;

int salary;

Teacher()

Teacher(String name,int age,int salary)

{
this.name =name;

this.age = age;

this.salary = salary;

void role()

System.out.println("Employee");

//Inheritance

class Student extends Teacher

String name;

int age;

int roll_no;

Student(String name,int age,int roll_no)

this.name = name;

this.age = age;

this.roll_no = roll_no;

//override

void role()

System.out.println("Student");

}
}

public class Excep {

public static void main(String[] args)

Teacher t = new Teacher("vamsi",35,30000);

Student s = new Student("krishna",20,21);

s.role();

4. Write a java program convert json file into csv file.


Solution:-
package c1;
import java.io.*;
import java.nio.file.*;
import org.apache.commons.io.FileUtils;
import org.json.*;

public class Excep {

@SuppressWarnings("deprecation")
public static void main(String args[])
{

String jsonString;
JSONObject jsonObject;

try {

jsonString = new String(


Files.readAllBytes(Paths.get("file.json")));

jsonObject = new JSONObject(jsonString);

JSONArray docs
= jsonObject.getJSONArray("test");
File file = new File("C:\\Test.csv");
String csvString = CDL.toString(docs);
FileUtils.writeStringToFile(file, csvString);
}

catch (Exception e) {

e.printStackTrace();
}
}
}

5. Write a java program to convert csv to json file;


Solution:-
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonToCsvConverter {

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

JSONParser parser = new JSONParser();

Object obj = parser.parse(new FileReader("json_file.json"));

JSONArray jsonArray = (JSONArray) obj;

List<String[]> csvData = new ArrayList<>();

for (int i = 0; i < jsonArray.size(); i++) {


JSONObject jsonObject = (JSONObject) jsonArray.get(i);

String name = (String) jsonObject.get("name");


String age = (String) jsonObject.get("age");
csvData.add(new String[]{name, age});
}
FileWriter writer = new FileWriter("csv_file.csv");

for (String[] row : csvData) {


writer.write(String.join(",", row));
writer.write("\n");
}

writer.close();
}
}

You might also like