ASSIGNMENT NO.
NAME - SHREEYA PRAVIN JOSHI
ROLL NO -57
PRN - 2122000723
Q1)
import java.io.File;
import java.io.IOException;
class CreateFile {
public static void main(String[] args) {
try {
File f= new File("C:\\Users\\Lenovo\\Desktop\\Input.txt");
if (f.createNewFile()) {
System.out.println("File created: " + f.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Q2 insert the content using fileinput stream
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
class WriteToFile {
public static void main(String[] args) {
try {
FileInputStream mywriter =new FileInputStream("input.txt");
//FileWriter myWriter = new FileWriter("input.txt");
mywriter.write("kit college is best in western maharastra");
mywriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Q3read the data for input file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("input.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Q4 write the output.txt file using buffered class
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
class manish{
public static void main(String[] args)
throws IOException {
String manish= "Hello welcome to Scaler!";
try{
BufferedWriter f = new BufferedWriter(new FileWriter("output.txt"));
f.write(manish);
System.out.println("sucessfully.txt file.");
f.close();
}
catch (IOException e){
System.out.println(e);
}
}
}