You are on page 1of 3

EXPERIMENT-2 on Streams

2.1. WAP in Java program to create a text file and write data into it.
import java.util.*;
import java.io.*;
class file
{
public static void main (String s[])throws IOException
{
1-FileOutputStream f= new FileOutputStream("123.txt");
Scanner sc=new Scanner(System.in);
System.out.println("Enter the text and put @ for end:");
char ch;
while(true)
{
ch=(char)sc.next().charAt(0);
if(ch!='@')
2-f.write(ch);
else
break;
}
3-f.close();
System.out.println("the file is successfully saved");
}
}
Output:
Enter the text and put @ for end:
s
a
m
b
i
t
@
the file is successfully saved
Explanation: - This program will create a text file in the current working directory. We are supposed
to write data into it. But at a time, we should input one symbol, and finally, @ is pressed to save the
file. Line 1 creates a new text file with name-123.txt. If this file is already there, it is open and ready
for writing. The method in line 2 writes data into the file but one symbol at a time. Line-3 will close
the writing operation and save the file.
2.2(a) WAP to show time efficiency using Bufferstream (I/O). This program is with buffer.
package test;

import java.io.*;

public class withBuf {

public static void main(String[] args) {

String filePath = "C:\\Users\\dipna\\eclipse-workspace\\test\\src\\test\\file.txt"; // Full path to


the file which required to be opened. This path will vary system to system

long startTime = System.currentTimeMillis(); // Here time is logged for the first time

try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath))) { //


File is opened using FileInputStream and wrapped in BufferedInputStream

int bytesRead;

byte[] buffer = new byte[1024]; // Read in 1KB chunks

while ((bytesRead = inputStream.read(buffer)) != -1) { // File is traversed here

// Process the data (e.g., write to another file)

} catch (IOException e) {

e.printStackTrace();

long endTime = System.currentTimeMillis(); // Execution ending time is logged here

long duration = endTime - startTime; // Duration is evaluated by taking the interval of ending
and start time

System.out.println("Time taken with buffering: " + duration + " milliseconds");

}
2.2(b) WAP to show time efficiency using Bufferstream (I/O). This program is without
buffer.
package test;

import java.io.*;

public class withoutBuf {

public static void main(String[] args) {

String filePath = "C:\\Users\\dipna\\eclipse-workspace\\test\\src\\test\\file.txt"; // Full path to


the file which required to be opened

long startTime = System.currentTimeMillis(); // Here time is logged for the first time

try (InputStream inputStream = new FileInputStream(filePath)) { // File is opened using


FileInputStream

int bytesRead;

byte[] buffer = new byte[1024]; // Read in 1KB chunks

while ((bytesRead = inputStream.read(buffer)) != -1) { // File is traversed here

// Process the data (e.g., write to another file)

} catch (IOException e) {

e.printStackTrace();

long endTime = System.currentTimeMillis(); // Execution ending time is logged here

long duration = endTime - startTime; // Duration is evaluated by taking the interval of ending
and start time

System.out.println("Time taken without buffering: " + duration + " milliseconds");

You might also like