You are on page 1of 7

EX NO: 6A

FILE I/O EXCEPTIONS


DATE:
READ INPUT FROM JAVA CONSOLE

AIM:
To write a java program to read input from java console.
ALGORITHM:
Step 1: Start the program.
Step 2: Enter the data using BufferedReader.
Step 3: Create an object (s) for the BufferedReader and initialize its constructor with
System.in (standard input stream) in an InputStreamReader.
Step 4: Read the data using readLine().
Step 5: Print the data.
Step 6: Stop the program.
PROGRAM:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class javaprog
{
public static void main(String[] args) throws IOException
{
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any string: ");
String str = s.readLine();
System.out.println("Entered string is " + str);

}
}

OUTPUT:

RESULT:
For the above program, the output was verified successfully.
EX NO: 6B
READ A FILE LINE BY LINE AND STORE IT INTO A VARIABLE
DATE:

AIM:
Write a java program to read a file line by line and store it into a variable.
ALGORITHM:
Step 1: Start the program.
Step 2: In try block, Create an object(fr) for class FileReader and initialize its
constructor with a file path.
Step 3: Create an object (reader) for the BufferedReader and initialize its
constructor with the object “fr”.
Step 4: Declare a variable line to store the data read from the BufferedReader.
Step 5: Check the condition (line! =null) and read from BufferedReader
till the end of file.
Step 6: Close the reader.
Step 7: When any IO exception occurs, it is caught by the catch block.
Step 8: Stop the program

PROGRAM

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class javaprog


{
public static void main(String[] args)
{
try
{
FileReader f = new FileReader("C:\\Users\\Brindha\\Downloads\\eclipse-java\\helo\\src\\
Computer.txt");

BufferedReader reader = new BufferedReader(f);


String line = reader.readLine();
while (line != null)
{
System.out.println(line);

line = reader.readLine();
}
reader.close();
}
catch (IOException e)
{
System.out.println(e);
}
}}

Computer.txt

OUTPUT:

RESULT:
For the above program, the output was verified successfully.
EX NO: 6C
READ FROM A FILE AND WRITE INTO ANOTHER FILE
DATE:

AIM:
To write a java program to read first 3 lines from a file and write into another file
ALGORITHM:
Step 1: Start the program.
Step 2: In try block, Create an object(fr) for class FileReader and initialize its
constructor with the source file path.
Step 3: Create an object (reader) for the FileWriter and initialize its
constructor with the destination file path.
Step 4: Declare a variable line to store the data read from the FileReader.
Step 5: Repeat step 6 for(i = 1; i <= 3 && line != null; i++)
Step 6: Write the data into the destination file & increment to the next line.
Step 7: Close the reader & writer.
Step 8: When any exception occurs ,it is caught by the catch block.
Step 9: Stop the program.
PROGRAM:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
public class javaprog
{
public static void main(String args[])
{
BufferedReader br=null;
BufferedWriter bw=null;
try{
br = new BufferedReader(new FileReader("C:\\Users\\Brindha\\Downloads\\eclipse-java\\
helo\\file_1.txt"));
bw = new BufferedWriter(new FileWriter("C:\\Users\\Brindha\\Downloads\\eclipse-java\\
helo\\file_2.txt"));
String line = br.readLine();
for( int i = 1; i <= 3 && line != null; i++)
{
bw.write(line);
bw.write("\n");
line = br.readLine();
}
System.out.println("Lines are Successfully copied!");
br.close();
bw.close();
}
catch(Exception e)
{
System.out.println("Exception caught : " + e);
}
}
}

file_1.txt

OUTPUT:
file2.txt
RESULT:
For the above program, the output was verified successfully.

You might also like