Java Examples
Java Examples
Solution:-
package c1;
import java.io.*;
files[0] = file1;
files[1] = file2;
files[2] = file3;
mergeFiles(files, merge);
writer.write(line);
writer.newLine();
writer.write("\n");
} catch (IOException e) {
System.err.println("Error reading file: " + fileName);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
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;
try {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
} catch (FileNotFoundException e)
} catch (IOException e)
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()
{
this.name =name;
this.age = age;
this.salary = salary;
void role()
System.out.println("Employee");
//Inheritance
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");
}
}
s.role();
@SuppressWarnings("deprecation")
public static void main(String args[])
{
String jsonString;
JSONObject jsonObject;
try {
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();
}
}
}
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
writer.close();
}
}