06 Collection
06 Collection
CONCEPT OF COLLECTIONS
Content
❑ Collection
❑ Map
❑ File & IO
1
Collections in Java
• Collections in java are a set of classes and interfaces used to
support operations on sets of objects.
For example : Searching, sorting, adding, editing, deleting,… can be done by Java
Collections.
public boolean removeAll (Collection c) Removes all elements from the specified collection
public boolean retainAll (Collection c) Remove all except the specified collection
Write a program to
delete an element
TreeSet
TreeMap
InputStream OutputStream
Method Description
1) public void write(int)throws is used to write a byte to the current
IOException output stream.
2) public void write(byte[])throws is used to write an array of bytes to the
IOException current output stream.
3) public void flush()throws IOException flush current output stream.
4) public void close()throws IOException used to close the current output stream.
Byte Stream
• Byte stream used to read / write data bytes data (8 bit)
• FileInputStream and FileOutputStream is 2 layers Okay history use
often most through
Method Description
1. public abstract int read()throws Reads the next byte of data from the input
IOException stream. It returns -1 when the end of the file is
reached.
2. public int available()throws Returns an estimate of the number of bytes
IOException that can be read from the current input
stream.
3. public void close()throws IOException used to close the current input stream.
Byte Stream
import java.io.FileInputStream ;
import java.io.FileOutputStream ; Example: Copy file
import java.io.IOException ;
import java.io.BufferedInputStream ;
import java.io.BufferedOutputStream ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
try {
in = new FileReader ( "C:\java\file1.txt" );
out = new FileWriter ( "C:\java\file2.txt" );
int c ;
while (( c = in.read ()) != -1) {
out.write ( c );
}
} finally {
if ( in != null ) { in.close ();}
if ( out != null ) { out.close ();}
}
System.out.println ("Copied successfully!");
}
}
Useing Bytes and Character Streams
Using Byte Stream
• Suitable when processing binary data (such as image files, videos,
etc.)
• Byte Stream reading/writing classes are usually named
…InputStream/…OutputStream
String sCurrentLine ;
while((sCurrentLine = br.readLine()) != null ) {
contentBuilder.append(sCurrentLine).append( "\n" );
}
} catch (IOException e) {
throw new RuntimeException ( e );
}
System.out.println(contentBuilder.toString());
}
}
Read data from file into String variable
• useing Files.readAllBytes
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Paths ;
try {
byte [] bytes = Files.readAllBytes(Paths.get("C:\java\file1.txt"));
content = new String(bytes);
} catch(IOException e) {
throw new RuntimeException(e);
}
System.out.println(content);
}
}
Write string to file
• Sử dụng FileWriter và BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
// deserialize
// detect end of file using the FileInputStream object
try (FileInputStream fi =
new FileInputStream("C:\\java\\students.dat");
ObjectInputStream in = new ObjectInputStream(fi)) {
while (fi.available() > 0) {
inputStudents.add((Student) in.readObject());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
for (Student s : inputStudents) {
System.out.println(s);
}
}
}