• A stream is a sequence of data .
• A stream is a flowing sequence of characters.
Character and Byte Streams
File Class
• boolean exists():does the file exist
• boolean isFile(): is it a file
• boolean isDirectory: directory
• String[] list() :return the names of all files and directories in a directory
• String getName() :get the file or directory's nameString
• getPath() :get the file or directory's path
• list(): This method of Files class returns an array of strings naming the files and directories in the directory.
• file.listFiles(): This method returns an array of files and folders present in the specified directory with their pathname.
• isDirectory(): It checks whether the file is a directory or not
public interface FilenameFilter • used to filter directory listings in the list
method of class File.• accept• public boolean accept(File dir, String name)
– Tests if a specified file should be included in a file list.
– Parameters:• dir - the directory in which the file was found.• name - the name of the file.
– Returns:–true if and only if the name should be included
in the file list; false otherwise.
–
• accept
• public boolean accept(File dir, String name) – Tests if a specified file should be included in a file
list. – Parameters:
• dir - the directory in which the file was found.• name - the name of the file.
– Returns:• true if and only if the name should be included in the file
list; false otherwise.
Class FileInputStream
• FileInputStream: – This class is a subclass of Inputstream class – reads bytes from a specified file name .
• Int read(): – method reads a byte or array of bytes from the file .– returns -1 when the end-of-file has been reached.
• This class throws FileNotFoundException
• Constructor:– FileInputstream(File filename);
FilterInputStream
• The FilterInputStream class is the superclass of all of the input stream classes that filter input.
• Each of the subclasses of FilterInputStreamprovides additional functionality.
• Data streams are filtered streams that perform binary I/O operation on primitive data type values ( boolean, char, byte, short, int, long, etc.) as well as on String values.
• If we need to work with data that is not represented as bytes or characters then you can use Data Streams.
• These streams filter an existing byte stream so that each primitive data types can be read from or written to the stream directly.
DataInputStream:
• allows you to read binary represented data of Java primitive data types from an underlying input stream in a machine-independent way
• DataoutputStream:• allows you to write binary represented data of
Java primitive data types reading from an underlying output stream in a machine-independent way. It writes only Java primitive data types and doesn't write the object values.
BufferedInputStream
• BufferedInputStream class that lets you read characters from a stream and stores it in an internal buffer .
• BufferedInputStream(InputStream inputStream) BufferedInputStream(InputStream inputStream, int bufSize)
BufferedoutputStream
• and buffers the output.
• BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("file.java"));
Random File Access
• Random access files permit nonsequential, or random, access to a file's contents .
• read(ByteBuffer) – Reads bytes into the buffer from the channel
• write(ByteBuffer) – Writes bytes from the buffer to the channel .
• Seek :sets the file pointer • getFilePointer () :reads filePointer.• Int skipBytes():Add n to the file pointer.
StreamTokenizer• Many times when reading an input stream of
characters, we need to parse it to see if what we are reading is a word or a number. This process is called "tokenizing".
• The StreamTokenizer class takes an input stream and parses it into "tokens“, allowing the tokens to be read one at a time.
• StreamTokenizer breaks the input stream into tokens using whitespace as a delimiter.
• By default, Unicode characters \u0000 through \u0020 are considered whitespace. This encompasses things like space, tab, newline, etc
• If you want to change this list, you need to invoke the method whitespaceChars(int low, int high); all characters having Unicode values between low and high will be considered whitespace, in addition to the default set.
• int nval :If the current token is a number, this field contains the value of that number.
• String sval :If the current token is a word token, this field contains a string giving the characters of the word token.
• nextToken( )• next token is obtained from the input
stream by calling
• int ttype After a call to the nextToken method, this field contains the type of the token just read.
• Int lineno() Return the current line number.
• void wordChars(int low, int hi) Specifies that all characters c in the range low <= c <= high are word constituents.
• Void ordinaryChars(int low, int hi) Specifies that all characters c in the range low <= c <= high are "ordinary" in this tokenizer.
• TT_EOF, TT_EOL, TT_NUMBER, and TT_WORD are four int constants
• TT_WORD indicates that the token is a word. • TT_NUMBER indicates that the token is a
number. • TT_EOL indicates that the end of line has been
read. The field can only have this value if the eolIsSignificant method has been called with the argument true.
• TT_EOF indicates that the end of the input stream has been reached.
• whitespaceChars( ) :Specify white space chars
StreamTokenizer
• eolIsSignificant( ) method to ensure that newline characters will be delivered as tokens, so we can count the number of lines as well as words.
• void eolIsSignificant(boolean eolFlag)• void wordChars(int start, int end)• start and end specify the range of valid
characters. characters in the range 33 to 255 are valid word characters.