Java Assignment 9 S 59
Java Assignment 9 S 59
ROLL NO -59
PRN - 2122000732
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();
}
}
}
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();
}
}
}
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
class manish{
public static void main(String[] args)
throws IOException {
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);
}
}
}