Interface Type and Its Implementation
Interface Type and Its Implementation
1. interfaces, the abstract data types that the framework supports. 2. implementations, the concrete versions of these interfaces. 3. algorithms, the predefined actions that can be defined on either the interfaces or their implementations. 1. All implementations are unsynchronized. 2. All implementations are serializable and cloneable 3. All implementations support having null elements. The predefined algorithms for supporting the framework are found in the Collections and Arrays classes.
The framework consists of four core interfaces with two specializations for sorting 1. Collection 2. List 3. Set 4. Map 5. SortedSet 6. SortedMap
LinkedList Class
The LinkedList class is a doubly linked list, which internally maintains references to the previous and n element at each node in the list. Creating a LinkedList 1. public LinkedList(): creating an empty list 2. public LinkedList(Collection col): copy constructor
lList.add("4"); lList.add("5"); System.out.println(lList); List lst = lList.subList(1, 4); System.out.println(lst); lst.remove(2); System.out.println(lst); System.out.println(lList); } }
Creating an ArrayList
1. For the first two constructors, an empty array list is created. 2. The initial capacity is ten unless explicitly specified by using the second constructor.
public ArrayList()
Input Output There are four types of streams: InputStream, OutputStream, Reader, and Writer. 1. Reader. A stream to read characters. 2. Writer. A stream to write characters. 3. InputStream. A stream to read binary data. 4. OutputStream. A stream to write binary data. For better performance, use the buffered I/O classes: BufferedInputStream, BufferedOutputStream, BufferedReader, and BufferedWriter. Reading from and writing to a stream dictate that you do so sequentially. The java.io.RandomAccessFile is for non-sequential operations. For object serialization and deserialization, you can use the ObjectInputStream and ObjectOutputStream classes.
.Reading Binary Data
1. FileInputStream enables easy reading from a file. 2. BufferedInputStream provides data buffering that improves performance. 3. The ObjectInputStream class is used in object serialization. 4.
| +--ObjectOutputStream
1. FileOutputStream provides a convenient way to write to a file. 2. BufferedOutputStream provides better performance. 3. ObjectOutputStream class plays an important role in object serialization. OutputStream The OutputStream class defines three write method overloads, which are mirrors of the read method overloads in InputStream:
public void write (int b) public void write (byte[] data) public void write (byte[] data, int offset, int length)
1. The first overload writes the lowest 8 bits of the integer b to this OutputStream. 2. The second writes the content of a byte array to this OutputStream. 3. The third overload writes length bytes of the data starting at offset offset. 1. close() method closes the OutputStream. 2. flush() method forces any buffered content to be written out to the sink. The File Class A File object represents a file or directory pathname. You can pass an absolute path to a file or directory like this:
File file1 = new File ("C:\\temp\\myNote.txt"); // in Windows File file2 = new File ("/tmp/myNote.txt"); // in Linux/Unix
FileInputStream
1. The FileInputStream class is a subclass of InputStream. 2. The FileInputStream class allows you to read binary data sequentially from a file. 3. The FileInputStream class's constructors allow you to pass either a File object or a path to a file.
public FileInputStream (String path) public FileInputStream (File file) import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class MainClass { public static void main(String[] args) { boolean areFilesIdentical = true; File file1 = new File("c:\\file1.txt"); File file2 = new File("c:\\file2.txt"); if (!file1.exists() || !file2.exists()) { System.out.println("One or both files do not exist"); System.out.println(false); } System.out.println("length:" + file1.length()); if (file1.length() != file2.length()) { System.out.println("lengths not equal"); System.out.println(false); } try { FileInputStream fis1 = new FileInputStream(file1); FileInputStream fis2 = new FileInputStream(file2); int i1 = fis1.read(); int i2 = fis2.read(); while (i1 != -1) { if (i1 != i2) { areFilesIdentical = false; break; } i1 = fis1.read(); i2 = fis2.read(); } fis1.close(); fis2.close(); } catch (IOException e) { System.out.println("IO exception"); areFilesIdentical = false; } System.out.println(areFilesIdentical); } }
class FileInputStreamDemo { public static void main(String args[]) throws Exception { FileInputStream fis = new FileInputStream(args[0]); // Read and display data int i; while ((i = fis.read()) != -1) { System.out.println(i); } fis.close(); } }
BufferedInputStream
For faster reading you should use BufferedInputStream to wrap any InputStream. BufferedInputStream has two constructors that accept an InputStream:
public BufferedInputStream(InputStream in) public BufferedInput Stream (Input Stream in, int bufferSize)
Then, instead of calling the methods on fis, work with the BufferedInputStream bis.