A stream is a sequence of data. A stream is a flowing sequence of characters.
-
Upload
benedict-taylor -
Category
Documents
-
view
230 -
download
2
Transcript of A stream is a sequence of data. A stream is a flowing sequence of characters.
![Page 1: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/1.jpg)
![Page 2: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/2.jpg)
• A stream is a sequence of data .
• A stream is a flowing sequence of characters.
![Page 3: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/3.jpg)
![Page 4: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/4.jpg)
![Page 5: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/5.jpg)
![Page 6: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/6.jpg)
Character and Byte Streams
![Page 7: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/7.jpg)
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
![Page 8: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/8.jpg)
• 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
![Page 9: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/9.jpg)
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.
–
![Page 10: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/10.jpg)
• 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.
![Page 11: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/11.jpg)
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);
![Page 12: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/12.jpg)
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.
![Page 13: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/13.jpg)
![Page 14: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/14.jpg)
• 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.
![Page 15: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/15.jpg)
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.
![Page 16: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/16.jpg)
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)
![Page 17: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/17.jpg)
BufferedoutputStream
• and buffers the output.
• BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("file.java"));
![Page 18: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/18.jpg)
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.
![Page 19: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/19.jpg)
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
![Page 20: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/20.jpg)
• 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.
![Page 21: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/21.jpg)
• 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
![Page 22: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/22.jpg)
• 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.
![Page 23: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/23.jpg)
• 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
![Page 24: A stream is a sequence of data. A stream is a flowing sequence of characters.](https://reader031.fdocuments.us/reader031/viewer/2022013004/56649f535503460f94c77cf8/html5/thumbnails/24.jpg)
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.