Sunday, 4 June 2017

JAVA Tutorial-7

1. Write a Main method that takes the name of a text file as a command line argument and prints every line in lower case.

 
import java.io.*;
class Fileread
{
 public static void main(String s[])throws Exception
 {
  FileInputStream in =new FileInputStream(s[0]);
  int size=in.available();
  int i;
  char c;
  for(i=0;i<size;i++)
  {
   c=(char)in.read();
   System.out.print(Character.toLowerCase(c));
  }
  in.close();
 }
}

Output

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

Share:

0 comments:

Post a Comment