2. Write a main() method that counts the number of words in a text file whose name is
accepted from standard input. Also print the size of a file.
import java.util.*;
import java.io.*;
class Filecount
{
public static void main(String s[])throws Exception
{
FileInputStream in =new FileInputStream(s[0]);
Scanner sc=new Scanner(in);
int size=in.available();
int count=0;
while(sc.hasNext())
{
sc.next();
count++;
}
System.out.println("Size of File: "+size);
System.out.println("There are "+count+" Word in file");
in.close();
}
}
Output
3. Write a program using BufferedInputStream, FileInputStream, BufferedOutputStream,
FileOutputStream to copy Content of one file File1.txt into another file File2.txt.
import java.io.*;
class Filecopy
{
public static void main(String s[])throws Exception
{
FileInputStream in = new FileInputStream("File1.txt");
BufferedInputStream bin = new BufferedInputStream(in);
FileOutputStream ou = new FileOutputStream("File2.txt");
BufferedOutputStream bou = new BufferedOutputStream(ou);
int size=in.available();
int i;
for(i=0;i<size;i++)
{
bou.write((char)bin.read());
}
System.out.println("Content of one file is copied to another fle");
bou.close();
ou.close();
bin.close();
in.close();
}
}
Output
0 comments:
Post a Comment