I/O

PREVIOUS                                                                                                                      NEXT



FILE

1    File is a collection of data stored in a particular area on the disk.
2    File is used to  read and write operations on the file.
3    A file program involves either or both of the following kinds of data communication.

1.  Data transfer b/w the console unit and the program.
2.  Data transfer b/w the program and a disk file.


I/p:

Keyboardàconsole
Consoleàfile
Fileàclass

O/p:

Classàfile
Fileàconsole
Consoleàdisplay


File is class of the java.io package that references an actual disk file.

File obj=new File(“path,file-name”);
File Obj=new File(“filename”);


Methods

getName()               à   Returns the name of the file
getAbsolutePath()    à   Returns the name of the parent directory
getParent()               à   Returns the directory only
getModified()            à   if it is modified or not
isHidden()                 à   Returns if the file hidden or not
canWrite()                à   Returns if the file is writable or not
canRead()                à   Returns if the file is readable or not
exist()                       à   Returns true if the file is exist or not false
isFile()                      à   Returns if it is file
isDirectory()             à   Returns if it is directory
length()                    à   Find the length of the string

Example for above methods:

import java.io.*;
class fdetail
{
public static void main(String s[])
{

//File ob=new File("c:/ani/abs.java");

File ob=new File(s[0]);

if(ob.exists())
{                              
System.out.println(ob.getName());
System.out.println(ob.getPath());
System.out.println(ob.getParent());
System.out.println(ob.getAbsolutePath());
System.out.println(ob.canRead()?"Readble":"not Readble");
System.out.println(ob.canWrite()?"Writable":"not Writable");
System.out.println(ob.isHidden()?"Hidden":"not Hidden");
System.out.println(ob.lastModified());
System.out.println(ob.isFile());
System.out.println(ob.isDirectory());
System.out.println(+f1.length());
}
else
System.out.println("File not Found");
}
}
}

com: javac fdetail.java
run:  java fdetail anu.txt



/* mkdir() --> to Create a Directory */

import java.io.*;
class mkd
{
public static void main(String s[])
{
File ob=new File(s[0]);

if(ob.mkdir())
        System.out.println("created");
else
        System.out.println("Already exist");
}
}

Example :
import java.io.*;

class dirdemo
{
public static void main(String s1[])
{
File ob=new File(s1[0]);
String start=s1[1];
String end=s1[2];

if(ob.isDirectory()) 
{
        String s[] = ob.list();
                   
        for(int i=0;i<s.length;i++)
                if(s[i].endsWith(end))
                  if (s[i].startsWith(start))
                        System.out.println(s[i]+"\t");
}
else
        System.out.println("Not a Directroy");
}
}

com: javac  dirdemo.java
run: java dirdemo pack p java
p1.java
p2.java
pack.java

packàdirectory
pàfile name represent
javaàextension

Streams

A Stream is a path of communication between the source and destination.

Stream are divided into 4 abstract classes

1.  Input stream
2.  Output stream
3.  Reader
4.  Writer

·       The I/o and o/p are designed byte stream
·       Reader and Writer are designed for characters stream
·       Byte stream classes and the character stream classes
    Form separate hierarchy

Byte Stream:

1. The byte stream classes provide a rich environment for   
     handling byte oriented I/O.

2. A byte stream can be used with any type of object
        including binary data.


File InputStream 

Class helps in reading data from the actual disk file.

FileInputStream o=new FileInputstream(string name);
FileInputStream o=new FileInputstream(File-obj);

Methods

Int read() àReturns and integer representation of the next available byte of I/p, -1 is return when the end of the file is 0ncountered.

available()àReturns the number of bytes that can be read from the file input stream.

close()àCloses the file input stream and releases any system resources associated with the stream.

Skip(long value)  àskip from specified value

import java.io.*;

class Fdemo
{
public static void main(String ar[])throws Exception
{
int t,i;

File ob=new File(ar[0]);
if(ob.exists())
{
FileInputStream in=new FileInputStream(ob);

t=in.available();
        System.out.println("Available:"+ t);

for(i=0;i<200;i++)
        System.out.print((char)in.read());
         System.out.println("\nAvailable:"+ in.available());

in.skip(400);
        System.out.println("Available:"+ ( t=in.available()) );

        while(in.read()!=-1)
               System.out.print((char)in.read());
               in.close();
   }
else
        System.out.println("File not Found");
}
}

run:  java fdemo fr.java


FileOutputStream
class helps to create a file and write data into it using the methods of the OutputStream class.

FileOutputStream ob=new FileOutputStream(“filename”,true);

Methods

write()              -       Writes the specified byte to the file output stream.


// FileOutputStream

import java.io.*;

class fwrite
{
public static void main(String s[])throws Exception
{
String st="This File Write Program";
int i,c;

FileOutputStream out=new FileOutputStream(s[0],true);

for(i=65;i<91;i++)
out.write(i);
out.write('\n');


do
{
c=System.in.read();
out.write(c);
}while(c!='#');
out.close();
              
out=new FileOutputStream(s[1]);
byte b[]=st.getBytes();

out.write(b,0,9);
out.write('\n');

out.write(b);
out.close();
}
}


Run:
Java fwrite  a1 a2
Hai
#
à
         type a1
         type a2


PREVIOUS                                                                                                                        NEXT

No comments:

Post a Comment