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:

Related Posts:

  • JAVA Tutorial-3 Create an array of String variables and initialize the array with the names of the months from January to December. Create an array containing 12 random decimal values bet… Read More
  • JAVA Tutorial-4 Program 1 Program 2 Program 3 Program 4 Program 5 1. create a class called student having data member like name,enr,branch,city,spi define construct which can ini d… Read More
  • Networking Program Network Program List Client & Server Using TCP Connection Client & Server Using UDP Connection … Read More
  • JAVA-Concept Wise Program Program List Class Program Stack Program Recursion Method Overloading Constructor & Constructor Overloading This Keyword Static variable, Static Method & Static Bloc… Read More
  • JAVA Tutorial-2 Write a program to display a random choice from a set of six choices for breakfast (you could use any set; for example, scrambled eggs, waffles, fruit, cereal, toast, or … Read More

0 comments:

Post a Comment