Professional Documents
Culture Documents
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.*;
long startTime = System.currentTimeMillis(); // Here time is logged for the first time
int bytesRead;
} catch (IOException e) {
e.printStackTrace();
long duration = endTime - startTime; // Duration is evaluated by taking the interval of ending
and start time
}
2.2(b) WAP to show time efficiency using Bufferstream (I/O). This program is without
buffer.
package test;
import java.io.*;
long startTime = System.currentTimeMillis(); // Here time is logged for the first time
int bytesRead;
} catch (IOException e) {
e.printStackTrace();
long duration = endTime - startTime; // Duration is evaluated by taking the interval of ending
and start time