- ByteStream is the basic I/O stream. Handles data as a stream of bytes. Does operation with byte unit and uses 8 bit. FileInputStream, FileOutputStream – can be used to copy files as byte by byte.
- Character Streams: FileReader and FileWriter are character streams. They treat file data as 16 bit unicode charater streams.
- InputStreamReader, OutputStreamWriter are also character streams. May be used in socket data reading.
- Line-Oriented I/O: BufferedReader and PrintWriter
- Buffered Streams: BufferedInputStream and BufferedOutputStream create buffered byte streams, while BufferedReader and BufferedWriter create buffered character streams
- You can set autoflush() of buffered streams or call manually flush() method.
- The scanner API breaks input into individual tokens. The formatting API assembles data into nicely formatted, human-readable form.
- Scanner:
Scanner scanner = new Scanner(new BufferedReader(new FileReader("test.txt"))); while (s.hasNext()) { System.out.println(s.next()); }
- To create formatted output streams, instantiate PrintWriter not PrintStream
- Check: System.out.format(“The square root of %d is %f.%n”, i, r);
- System.out and System.err are PrintStream objects. PrintStream is really a byte stream but uses some mechanism to emulate character stream.
- InputStreamReader cin = new InputStreamReader(System.in); System.in is a byte stream. InputStreamReader is used to emulate a character stream.
- System.console provides character streams to handle console read/write operations. Provides readPassword method to read password from the console
- Data Streams: Support I/O operations of primitive data types. DataInputStream and DataOutputStream.
- Check:
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));out.writeDouble(prices);out.writeInt(units);out.writeUTF(descs);
- DataStreams detects an end-of-file condition by catching EOFException
- Correct type to represent currency values: java.math.BigDecimal – an object type
- Object Streams: ObjectInputStream and ObjectOutputStream. Use writeObject and readObject to write and read objects respectively.
- A stream can contain only one copy of an object but many references to it when required.
- File Objects: File I/O
- Random access file:
new RandomAccessFile("test.txt", "r");new RandomAccessFile("test.txt", "rw");RandomAccessFile methods: * int skipBytes(int) * void seek(long) * long getFilePointer()
- The new java.nio.* package provides supports to handle file i/o for high performance applications.
From: http://sitestree.com/?p=4878
Categories:Java Short Notes, SCJP
Tags:
Post Data:2012-10-24 02:25:07
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada