I/O Files - Java

Java programs perform I/O through streams.Astream is an abstraction that either produces or consumes information a stream is linked to a physical device by the java I/O system.Java implements streams within class hierarchies defined in the java.io package.
               Java 2.0 defines two types of streams they are byte and char.byteStreams provide aconvinient means for handling input and output  of bytes.byteStreams are used for example when reading or writing binary data.
              character streams provide a convenient means for handling input and output of chars. character streams are more efficient than byte streams.

 

BYTESTREAM CLASSES CHARACTERSTREAM CLASSES
BufferedInputStream BufferedReader
BufferedOutputStream BufferedWriter
ByteArrayInputStream CharArrayReader
ByteArrayOutputStream CharArrayWriter
DataInputStream FileReader
DataOutputStream FileWriter
FileInputStream FilterReader
FileOutputStream FilterWriter
FilterInputStream InputStreamReader
FilterOutputStream InputStreamWriter
InputStream OutputStreamWriter
OutputStream PipedReader
PipedInputStream PipedWriter
PipedOutputStream PrintWriter
PrintStream PushBackReader
RandomAccessFile Reader
SequenceInputStream StringReader
  StringWriter
  Writer

WRITE A PROGRAM TO READ A CHRACTER FROM THE BUFFERED READER.

import java.io.*;
class Sample
{
public static void main(String args[]) throws IOException
{
char c;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter q to Quit");

 

          do
{
c=(char) b.read();
System.out.println(c);
}
while(c!='q');
}
}

WRITE A PROGRAM TO READ THE STRING USING BUFFRED READER.

import java.io.*;
class Sample
{
public static void main(String args[]) throws IOException
{
String c;
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Quit to Quit");
do
{
c=b.readLine();
System.out.println(c);
}
while(!c.equals("Quit"));
}
}

WRITE A PROGRAM ON PRINT WRITER CLASS.

import java.io.*;
class Sample
{
public static void main(String args[]) throws IOException
{
PrintWriter pw=new PrintWriter(System.out,true);
pw.println("Welcome");
pw.println("20");
pw.println("vision");
}
}

WRITE A PROGRAM TO READ THE DATA FROM TEXT FILE

import java.io.*;
class Sample
{
public static void main(String args[]) throws IOException
{
int c;
FileInputStream fin;
try
{
fin=new FileInputStream(args[0]);
}
catch(FileNotFoundException e)
{
System.out.println("File not found");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage:show file");
return;
}
do
{
c=fin.read();
if(c!=-1)
System.out.println((char)c);
}
while(c!=-1);
fin.close();
}
}

WRITE A PROGRAM TO COPY THE TEXT FROM ONE FILE TO ANOTHER FILE.

import java.io.*;
class Sample
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
try
{
try
{
fin=new FileInputStream(args[0]);
}
catch(FileNotFoundException e)
{
System.out.println("Input file not found");
return;
}
try
{
fout=new FileOutputStream(args[1]);
}
catch(FileNotFoundException e)
{
System.out.println("Error opening output file");
return;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: copy file from to");
return;
}
try
{
do
{
i=fin.read();
if(i!=-1)
fout.write(i);
}
while(i!=-1);
}
catch(IOException e)
{
System.out.println("File Error");
}
fin.close();
fout.close();
}
}

WRITE A PROGRAM ON FILE CLASS.

import java.io.File;
class Sample
{
public static void main(String args[])
{
File F=new File("d:/jdk1.3/pln");
System.out.println("Name: "+F.getName());
System.out.println("Path: "+F.getPath());
System.out.println("AbsolutePath: "+F.getAbsolutePath());
System.out.println("Parent: "+F.getParent());
System.out.println("Exist: "+F.exists());
System.out.println("Canwrite: "+F.canWrite());
System.out.println("Isabsolute: "+F.isAbsolute());
System.out.println("Length: "+F.length());
System.out.println("Canread: "+F.canRead());
System.out.println("Isdirectory: "+F.isDirectory());
System.out.println("Isfile: "+F.isFile());
System.out.println("Lastmodified: "+F.lastModified());
}
}

WTITE A PROGRAM TO DISPLAY THE FILES AND DIRECTORIES.
import java.io.File;
class Sample
{
    public static void main(String args[])
    {
       String dn="d:/jdk1.3/pln";
       File f1=new File(dn);
       if(f1.isDirectory())
       {
        System.out.println("Directory Of"+dn);
        String s[]=f1.list();
        for(int i=0;i<s.length;i++)
        {
           File f=new File(dn+"/"+s[i]);
           if(f.isDirectory())
           {
             System.out.println(s[i]+"is a directory");
           }
           else
           {
             System.out.println(s[i]+"is a file");
           }
         }
       }
       else
       {
             System.out.println(dn+"is not a directory");
       }

     }
}

WRITE A PROGRAM ON FILE FILTER.

import java.io.*;
class Abc implements FilenameFilter
{
String c;
public Abc(String x)
{
this.c="."+x;
}
public boolean accept(File d,String n)
{
return n.endsWith(c);
}
}
class Sample
{
public static void main(String args[])
{
String d="d:/jdk1.3/pln";
File f1=new File(d);
FilenameFilter o=new Abc("java");
String s[]=f1.list(o);
for(int i=0;i<s.length;i++)
{
System.out.println(s[i]);
}
}
}

WRITE A PROGRAM ON UNREAD.

import java.io.*;
class Sample
{
public static void main(String args[]) throws IOException
{
String s="if(a==4)a=10;\n";
byte b[]=s.getBytes();
ByteArrayInputStream in=new ByteArrayInputStream(b);
PushbackInputStream f=new PushbackInputStream(in);
int c;
while((c=f.read())!=-1)
{
switch(c)
{
case '=':
if((c=f.read())=='=')
System.out.println(".eq.");
else
{
System.out.println("<-");
f.unread(c);
}
break;
default:
System.out.println((char)c);
break;
}
}
}
}

WRITE A PROGRAM ON FILE READER.

import java.io.*;
class Sample
{
public static void main(String args[]) throws Exception
{
FileReader f=new FileReader("d:/jdk1.3/pln/hello.java");
BufferedReader br=new BufferedReader(f);
String s;
while((s=br.readLine())!=null)
{
System.out.println(s);
}
f.close();
}
}

WRITE A PROGRAM ON FILE WRITER.

import java.io.*;
class Sample
{
public static void main(String args[]) throws Exception
{
String s="Welcome to vision";
char b[]=new char[s.length()];
s.getChars(0,s.length(),b,0);
FileWriter f=new FileWriter("d:/jdk1.3/pln/hello.java");
for(int i=0;i<b.length;i++)
{
f.write(b[i]);
}
f.close();
}
}

 

For
More Explanation
&
Online Classes

Contact Us:
+919885348743