Gramin Technical And Management Campus
Department Of Computer Engineering
Subject/code: JPR/22412
Name: Thadke Priyanka Batch: A Roll No: 85
Practical No:31 & 32
Aim:- Develop a program for implementation of I/O stream and file stream
classes.
Program code:-
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class StreamExample {
public static void main(String[] args) {
String inputFile = "input.txt";
String outputFile = "output.txt";
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
String data = "Hello, world!";
byte[] byteData = data.getBytes();
outputStream.write(byteData);
System.out.println("Data written to " + outputFile);
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
try (FileInputStream inputStream = new FileInputStream(inputFile)) {
int byteRead;
System.out.print("Data read from " + inputFile + ": ");
while ((byteRead = inputStream.read()) != -1) {
System.out.print((char) byteRead);
}
} catch (IOException e) {
System.out.println("Error reading from file: " + e.getMessage());
}
}
}
Output:-
Exercise:-
Q.1)Develop a program to copy the characters from one file to another.
Program code:-
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCopy1
{
public static void main(String[] args)
{
String sourceFile = "Input.txt";
String destinationFile = "Output.txt";
try {
FileReader reader = new FileReader(sourceFile);
FileWriter writer = new FileWriter(destinationFile);
int character;
while ((character = reader.read()) != -1)
{
writer.write(character);
}
reader.close();
writer.close();
System.out.println("File copied successfully!");
} catch (IOException e) {
System.err.println("Error occurred: " + e.getMessage());
System.out.println();
}
}
}
Output:-
Q.2) Develop a program to write bytes to a file.
Program code
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteWriter
{
public static void main(String[] args)
{
String fileName = "Output.bin";
byte[] bytesToWrite = {65, 66, 67, 68, 69};
try {
FileOutputStream outputStream = new FileOutputStream(fileName);
outputStream.write(bytesToWrite);
outputStream.close();
System.out.println("Bytes have been written to the file successfully.");
}
catch (IOException e)
{
System.out.println("An error occurred while writing bytes to the file: " +
e.getMessage());
System.out.println(e);
}
}
}
Output:-
Q.3)Develop a program to display the content of file supplied as command
line arguments in java.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileContentReader
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.out.println("This is the source file");
return;
}
String filePath = args[0];
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
bufferedReader.close();
}
catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
System.out.println(e);
}
}
}
Output:-