My other sites |
New Summer Look :)
![]() |
Í![]() |
|
| - Java hides the operating system from you, so that you can manipulate data as streams in a device/operating system independent way |
![]() |
|
|
- Abstract base classes for input and output functionality - Defines basic set of methods - Used to get data from files, other objects, etc Two base structures: |
|
InputStream Methods |
OutputStream Methods |
| int read() int read(byte[] ) int read(byte[], int, int) void close int available() skip(long) |
void write(int) void write(byte[] ) void write(byte[], int, int) void close() void flush() |
|
- FileInputStream -> ~ path to file - FileOutputStream - DataInputStream - byte readByte(), long readLong(), double readDouble() - DataOutputStream - writeByte(byte), writeLong(long), writeDouble(double) - PipedInputStream - PipedOutputStream |
|
- 16 bit version of Streams for UNICODE - InputStreamReader(is) - OutputStreamWriter(os) - BufferedReader(ir) - BufferedWriter(iw) - StringReader(string) - StringWriter() |
import java.io.* ;
public class CharInput {
public static void main(String[] argv) {
String s;
InputStreamReader ir;
BufferedReader in;
ir = new InputStreamReader(System.in)
in = new BufferedReader(ir);
// Can you figure where the try clause needs to go?
while ((s = in.readLine()) !=null) {
System.out.println("Echo line:"+s);
}
}
}
|
|
- Platform independent definition of file&directory names |
| Often Used Methods: - String getName(); - String getPath(); - String getAbsolutePath(); - String getParent(); - boolean renameTo(File); - long lastModified(); - long length(); - boolean delete(); - boolean exists(); - boolean canWrite(); - boolean canRead(); - boolean isFile(); - boolean isDirectory(); - boolean mkdir(); - String[] list(); |
![]() |
![]() |
For example, we want an output stream that is both compressed & encrypted new EncryptedOutputStream( |
| - Provides a simple lexical analyzer - Analyzes an input stream or a reader - Breaks the input up into tokens - Same concept as StringTokenizer but on a stream - use int nextToken() to get the next token; this is returned into either sval or nval depending on the type of the token - determine the type of the token - TT_EOF, TT_EOL, TT_NUMBER, TT_WORD - eolIsSignificant(), commentChar() |
![]() |
- Ability to save a java object to a stream - Implementing Serializable enables serialisation - No methods or fields, just a semantic construct - tag - NotSerializableException is thrown if the object graph has any non serializable objects - transient modifier - Can all classes be serialised? |
| private void writeObject(java.io.ObjectOutputStream
out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; |
import java.io.*;
public class SerializeTest {
SerializeTest() {
String st = new String("This is the message!");
try {
FileOutputStream f = new FileOutputStream("c:\\test.ser");
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(st);
s.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String args[]) { new SerializeTest(); }
}
|
public class UnserializeTest {
UnserializeTest() {
String st;
try {
FileInputStream f = new FileInputStream("test.ser");
ObjectInputStream s = new ObjectInputStream(f);
st = (String) s.readObject();
s.close();
}
catch (Exception e) {
System.out.println(e.getMessage()); }
System.out.println("String:" + st);
}
public static void main(String args[]) {
new UnserializeTest();
}
}
|
|
1. Why Integer.parseInt(String) is static ? public class TestEquality {
public static void main(String[] args) {
String string1 = new String("Equality");
String string2 = new String("Equality");
if (string1 == string2)
System.out.println("string1 == string2");
if (string1.equals(string2))
System.out.println("string1 equals string2");
}
}
3. Does it make sense to null terminate \0 a String in Java? 4. How would you get a random number between 1 and 10? |
![]() |
1. Write a small program to simulate the throwing of a six-faced dice, using the java.util.Random Class 2. Use the java.lang.System class to obtain the following
system properties. |