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

Java Assignment 9 S 59

The document contains Java code snippets for file operations, including creating a file, writing to a file using FileInputStream, reading from a file with a Scanner, and writing to an output file using BufferedWriter. Each section is labeled with a question number and demonstrates basic file handling techniques in Java. The code includes error handling for potential exceptions during file operations.

Uploaded by

anishwarushe0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Assignment 9 S 59

The document contains Java code snippets for file operations, including creating a file, writing to a file using FileInputStream, reading from a file with a Scanner, and writing to an output file using BufferedWriter. Each section is labeled with a question number and demonstrates basic file handling techniques in Java. The code includes error handling for potential exceptions during file operations.

Uploaded by

anishwarushe0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

ASSIGNMENT NO.

NAME - SANKET BHIMAPA KOLI

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();
}
}
}

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);
}
}
}

You might also like