- File
It deals directly with files and the filesystem and does not specify how information is retrieved or stored in a file. It however provides properties of the file.
File ConstructorsFile (String DirectoryPath)
File (String DirectoryPath, String Filename)
File(File dirObj, String Filename)
File (URI uriObj)
File Methods
getName()
getpath()
getAbsolutePath()
getParent()
boolean canWrite()
boolean canRead()
boolean isHidden()
boolean isFile()
long length()
boolean isDirectory()
boolean setReadOnly()
It is a file that contains a list of other files and directories.
Methodslist()
The listFiles( ) AlternativeFile[ ] listFiles( )
File[ ] listFiles(FilenameFilter FFObj)
File[ ] listFiles(FileFilter FObj)
Directory
These methods return the file list as an array of File objects instead of strings.
The third version of listFiles( ) returns those files with path names that satisfy the
specified FileFilter. FileFilter defines only a single method, accept( ), which is called
once for each file in a list. Its general form is given here:
boolean accept(File path)
FilenameFilter InterfaceThe lists can implement interface to return file lists of certain extension. It implements the accept method.
public class File01 implements FilenameFilter
{
public boolean accept(File dir,String name){ return (name.endsWith("exe") name.endsWith("java")); }
public static void main(String args[])
{
File01 ff = new File01();
File f1 = new File("C:\\Program Files\\Java\\jdk1.5.0_14\\abc");
String s[]=f1.list(ff);
________________________________________________________
FileInputStreamThe FileInputStream class creates an InputStream that you can use to read bytes from
a file. Its two most common constructors are shown here:
FileInputStream(String filepath)
FileInputStream(File fileObj)
Either can throw a FileNotFoundException
FileOutputStreamFileOutputStream creates an OutputStream that you can use to write bytes to a file. Its
most commonly used constructors are shown here:
FileOutputStream(String filePath)
FileOutputStream(File fileObj)
FileOutputStream(String filePath, boolean append)
FileOutputStream(File fileObj, boolean append)
They can throw a FileNotFoundException or a SecurityException
________________________________________________________
ByteArrayStreams
ByteArrayInputStream
byte buff[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buff);
A ByteArrayInputStream implements both mark() and reset().
If mark is not called, then reset points to start of buffer.
ByteArrayOutputStreamByteArrayOutputStream is an implementation of outputStream that uses byte array as destination
ByteArrayOutputStream()
ByteArrayOutputStream(int numBytes)
String s ="This is a test";
byte buff[] = s.getBytes();
OutputStream f2 = new FileOutputStream("test.txt");
ByteArrayOutputStream f = new ByteArrayOutputStream();
//Method#1
f.write(buff);
f.writeTo(f2);
//Method#2
f2.write(buff);
________________________________________________________
Filtered Byte StreamsWrapper classes of InputStream and Output streams.
Constructors are
FilterOutputStream(OutputStream os)
FilterInputStream(OutputStream os)
Buffered Byte Streams useful in I/O performance optimization.
BufferedInputStream
ConstructorsBufferedInputStream(InputStream inputStream)
BufferedInputStream(InputStream inputStream, int bufsize)
mark and reset() are supported
BufferedOutputStream
ConstructorsBufferedOutputStream(OutputStream outputStream)
BufferedOutputStream(OutputStream outputStream,int bufsize)
Example:
String s="test";
byte buf[]=s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
BufferedInputStream bufStream = new BufferedInputStream(in);
bufStream.read()
BufferedOutputStream outStream = new BufferedOutputStream(buf);
outStream.flush(); //Recommended to use flush than write to reduce I/O
________________________________________________________
No comments:
Post a Comment